Advanced DAX - Dynamic Segmentation, Time Comparisons, Cross Sell and Averages - Power BI Experience
Advanced DAX - Dynamic Segmentation, Time Comparisons, Cross Sell and Averages - Power BI Experience
avancado-
Português
comparac
medias/)
(https://powerbiexperience.com/)
Leonardo Karpinski
Power BI Master and Microsoft Certi ed Trainer, owner of the Power BI Experience. Graduated
more than 20,000 students and participated in extensive projects for global companies.
/
(https://po
avancado-
Português
comparac
medias/)
(https://powerbiexperience.com/)
Before I start, I’d like to ask a few questions. Have you ever had problems with analysis
used to compare current results with last year or other weeks? What about problems
with analysis where you have to compare speci c averages with the total? Or have you
I often receive questions on my social networks regarding these topics, and the subject
So, stick around and follow up until the end that you will be good at these advanced
DAX themes:
Cross sell
Tip:
that we have data from 2017 to 2019 in our database. And on the page where we will
do the analysis we lter the year 2018. Then, our “current” context will be 2018.
Faturamento =
Faturamento LY =
) )
Faturamento YoY =
Faturamento YoY % =
The abbreviations LY and YoY, come from Last Year (previous year) and Year over
A question….did you notice anything di erent in the 4 measures? Pay attention to the
And then, do you understand? I am using “,” instead of “;”. This was an option that
came in one of the last Power BI updates. And for those who follow me, they know
that I intend to promote my work in other countries! So, I’ve already started to adapt.
Well, with these measures we have already made some progress with the analysis in
/
(https://po
avancado-
Português
comparac
medias/)
(https://powerbiexperience.com/)
Stages:
S catter Plot
This is one I don’t use so much, but I started to focus on using it more often, because it
helps a lot the user to understand and see the whole scenarios.
The rst step is to create the graph with the information we already have:
/
(https://po
Stages: avancado-
Português
comparac
In "Visualizações" select "Gráfico de dispersão" → Format the space a
medias/)
(https://powerbiexperience.com/)
It helps a lot if we put a parameter for the graphic subtitle that di ers if the growth
This is possible, but before we add this element to the view we need an auxiliary table
Stages:
/
Important: (https://po
avancado-
When using limits in measures you have to be very careful where you are going to usePortuguêscomparac
medias/)
smaller (<), larger (>) and equal (=). A lot of people make this mistake and complicate
(https://powerbiexperience.com/)
The next measure we will create is the one that makes the calculation of the
CALCULATE (
[Faturamento],
FILTER (
VALUES ( dCliente[Categoria] ),
mSup] )
Along with it we have the dynamic segmentation of the revenue per growth! Let’s see
if it works!? For that I will create a visual with the segmentation chart and with the
See that if we don’t apply the lter to create this virtual relationship in the measure
By doing so, we can improve our scatter plot by changing the X-axis measurement and
Another cool design that helps you understand the revenue is the stacked bar chart!
/
Figure 8: Stacked bar chart
(https://po
avancado-
To visualize the representation of our turnover by category and growth type, we will
Português
comparac
medias/)
(https://powerbiexperience.com/)
create a clustered column chart:
Stages:
Besides, imagine that the user wants a quickly view only the negative categories. What
could we do to build this into our design!? If you answered data/ lter segmentation,
/
(https://po
avancado-
Português
comparac
medias/)
(https://powerbiexperience.com/)
Stage:
I told you this classi cation is dynamic! Well, and then you can ask “Leo, why is this
classi cation dynamic!? I answer you! Because if you add lters and change contexts
on the page the visuals will interact with this new de nition! So, it’s not static….
For example, we will apply a lter for the “Sugar” group on the page:
/
(https://po
avancado-
Português
comparac
medias/)
(https://powerbiexperience.com/)
Stages:
See how much the stacked bar chart values have changed!
To nish our page, we will place a card indicating our summarized revenue variation:
/
(https://po
Stage: avancado-
Português
comparac
In "Visualizações" select "Multiple-line card" → Set up the space acc
medias/)
(https://powerbiexperience.com/)
The result of our rst page of YoY revenue analysis looks like this:
margin analysis, comes to add information about the business. So, in a new page we
We will need our cost to calculate the margin. The cost comes from the chart
“fVendas” and depends on the unit cost of the product and how many units were sold,
Custo =
And our margin measurement is tied to our revenue and our cost:
Margem =
[Faturamento] – [Custo]
/
(https://po
And to calculate the previous year’s margin, just use the SAMEPERIODLASTYEAR: avancado-
Português
comparac
medias/)
Margem LY =(https://powerbiexperience.com/)
Margem YoY =
We will start to analyze with the table design. With it, we will understand by category
how much we improved or worsened in relation to the previous year (remember that
/
(https://po
Stage: avancado-
Português
comparac
In "Visualizações" select "Tabela" → Set up the space according to th
medias/)
(https://powerbiexperience.com/)
We already see that we have worsened the margin, even having a bigger revenue
%).
Well, imagine you have presented this result to your manager, and he started to get
worried. And now what? How to help him understands this information!? Remember
The rst step is to create a scatter plot with the information of variation of margin and
Stage:
Well, with this chart you can better understand the analysis!? Di cult, right!? We still
have how to improve and a lot…. the second step is to divide this chart in 4 quadrants:
To make this division in the chart, we use the help of “Analysis” …. is that magnifying
Stages:
/
(https://po
avancado-
Português
comparac
medias/)
(https://powerbiexperience.com/)
This way, we already have a much better idea of where we should analyze in more
detail. Imagine that we can create a lter that makes the segmentation of these
quadrants. It would help, right? Yes, and I say more… let’s do it. Let’s create a
/
Figure 19: Segmentation chart
(https://po
avancado-
Português
comparac
medias/)
Stage: (https://powerbiexperience.com/)
In "Página Inicial" select "Inserir dados" → Fill chart as image
Important:
Let’s delete a relationship that Power BI automatically made between this and the
/
(https://po
Stage: avancado-
Português
comparac
In "Modelagem" select "Gerenciar relações" → "Excluir relação" betwee
medias/)
(https://powerbiexperience.com/)
Remember how we managed to use this kind of segmentation for invoicing!? We had
to put these limits within a measure for Power BI to apply context to each line. So,
CALCULATE (
[Margem YoY],
FILTER (
VALUES ( dCliente[Categoria] ),
Important:
Limits are applied and associated only for “Category” of the client! If you want to
create this analysis for customer table information, you must change the VALUES part
of the measurement.
With this measurement, we change the elds of our scatter plot by adding the subtitle
/
(https://po
avancado-
Português
comparac
medias/)
(https://powerbiexperience.com/)
Stage:
And we will also create 3 visuals to provide the user with information to analyze
customer categories and products with low margin and have a data segmentation per
quadrant:
/
(https://po
avancado-
Português
comparac
medias/)
(https://powerbiexperience.com/)
Stages:
With the visuals ready, we have our YoY margin analysis page ready, which looks like
this:
/
(https://po
avancado-
Português
comparac
medias/)
(https://powerbiexperience.com/)
Cross sell
Cross-sell analysis is well known for retail and distribution people. I don’t know if you
know that story of an American market that analyzed its sales and realized that on
Fridays the quantity of beer and diaper shopping on the same purchase was high. This
happened because normally the parents who were going to buy the diapers already
took the opportunity to take the weekend beer home. This is a very classic example of
Market Basket Analysis, and it helps users a lot to try to understand their sales.
The idea here is to quantify how many customers have bought a certain product A (e.g.
Clientes =
DISTINCTCOUNT ( fVendas[cdCliente] )
/
(https://po
avancado-
Português
comparac
medias/)
(https://powerbiexperience.com/)
Good, here we have the information we need (product and quantity). However, we
need the duplicate product information! Think with me, you will be analyzing a product
on one side in a chart and this has to be related (so two charts) with another product in
another chart.
You can create this chart virtually, but here we will duplicate the chart “dProduto” in
Power Query:
/
(https://po
avancado-
Português
comparac
medias/)
(https://powerbiexperience.com/)
Stapes:
With the duplicate table, we can close and apply to return to the Power BI desktop.
Remember what I said back there about what can happen when we create new chart in
the project? Power BI can create relationships automatically! In this case, it created the
relationship between the original product chart and the copy. We will need this
/
(https://po
avancado-
Português
comparac
medias/)
(https://powerbiexperience.com/)
Stage:
To calculate the customers who bought both from the group selected in the rst data
dProdAux1) we use the intersection between them. For this we will create a measure
using INTERSECT:
Clientes Cross =
VAR vClientes0 =
VALUES ( fVendas[cdCliente] )
VAR vClientes1 =
CALCULATETABLE (
VALUES ( fVendas[cdCliente] ),
ALL ( dProduto ),
uto] )
)
VAR vInt =
/
INTERSECT ( vClientes0, vClientes1 )
(https://po
avancado-
RETURN Português
comparac
COUNTROWS ( vInt ) medias/)
(https://powerbiexperience.com/)
We will apply two data segmentations on the page (Açucares to Grupo and Farinha de
Trigo to Grupo 1) and check the cross sell result for this analysis:
/
(https://po
avancado-
Português
comparac
medias/)
(https://powerbiexperience.com/)
Stages:
Having the measures of quantity of customers who bought the product and also the
cross sell. There is a way to evaluate the percentage relation between them with a
Confiança =
/
Figure 30: Cross sell matrix (con ança)
(https://po
avancado-
Português
comparac
medias/)
Stage: (https://powerbiexperience.com/)
In "Visualizações" select "Matriz" → Set up according to the image -
To leave a more intelligible visual to our user, we can build a heat map on the matrix:
Stage:
/
So, what do you think of the visual? Did you notice that when the line is equal to the
(https://po
column (“Açúcar” x “Açúcar”, “Óleo x Óleo”, etc…) the values are
100% and that gives Português
a avancado-
comparac
“polluted” visual? We can change the measure of “Con ança” so that the return is medias/)
(https://powerbiexperience.com/)
empty in these cases, greatly improving the visual:
Confiança =
IF (
Aux1[Grupo] ),
Well, now imagine that instead of analyzing 2 products you have 3. This was a question
of one of our students in the Telegram group when I was setting up Live #27, and that
/
(https://po
avancado-
Português
comparac
medias/)
(https://powerbiexperience.com/)
And now, we need to add in our measure a new intersection which is the result of the
VAR vClientes1 =
CALCULATETABLE (
VALUES ( fVendas[cdCliente] ),
ALL ( dProduto ),
uto] )
VAR vClientes2 =
CALCULATETABLE (
VALUES ( fVendas[cdCliente] ),
ALL ( dProduto ),
uto] )
VAR vInt01 =
VAR vInt12 =
RETURN
COUNTROWS ( vInt12 )
That’s it! With this we can apply 3 lters on the page (Grupo – dProduto, Grupo 1 –
dProdAux1 and Grupo 2 – dProdAux2) that our visuals will work for this context:
/
(https://po
avancado-
Português
comparac
medias/)
(https://powerbiexperience.com/)
Stages:
/
Average per categor y (https://po
avancado-
Português
comparac
This is an interesting analysis to evaluate how one or some categories behave
medias/)
(https://powerbiexperience.com/)
compared to the total.
As it is a comparison, we have to create two measures. The rst one will be the
average revenue per client, considering lters applied if the user wants to evaluate
some speci c group. And the second, the average revenue of all clients (disregarding
any lter applied to visuals) that we use ALL to disregard applied contexts.
Chart: with the group and the two measures to show the values
Column and Row Graph: to analyze how the variation in the result of the selected
/
(https://po
avancado-
Português
comparac
medias/)
(https://powerbiexperience.com/)
And the result with the applied lter (bakery, in the example) looks like this:
Stages:
We will build this counter in 2 steps. First we will create the number of the week in our
Semana do Ano =
WEEKNUM ( dCalendario[Data] )
Contador Semana =
/
(https://po
avancado-
Português
comparac
medias/)
(https://powerbiexperience.com/)
Stage:
3. Repeat step 1
With this column to help, we were able to create our comparative measures for the
Faturamento LW =
CALCULATE (
[Faturamento],
FILTER (
ALL ( dCalendario ),
dCalendario[Contador Semana]
VAR vSemanaContexto =
RETURN
CALCULATE (
[Faturamento]
mana] ), fVendas ),
FILTER (
ALL ( dCalendario ),
– 4
/
(https://po
avancado-
Português
comparac
medias/)
(https://powerbiexperience.com/)
Stages:
Well, guys, that was our Live #27 content!! A lot of what I did here were questions and
suggestions from the people in the social networks. So, I hope it helped you to
understand these analyses that are at a more advanced level of DAX. If you have any
questions or suggestions for the next Lives topics, you already know, you can leave
Cheers,
Leonardo.
/
(https://po
avancado-
Português
comparac
Share this post:
(https://powerbiexperience.com/)
medias/)
(https://powerbiexperience.com/en/using (https://powerbiexperience.com/en/how-
Add a comment...
/