Forum Discussion
DAX || Power BI || Calaculate
- 1 year ago
Answers are below, based on the the below code
dax1)
month_selection = SELECTEDVALUE('calendar'[month_no],0)
dax2)calculate_new_subs = CALCULATE(sum(net_subscriber_growth[NewSubscribers]),FILTER(net_subscriber_growth,MONTH(net_subscriber_growth[Month]) = [month_selection]))1)The first question would be why i am getting calcaulate_new_subs value for all month insite i have not clicked anything in the slicer.
Ideally 0 should have been supplied according to the dax and blank should be returned in the matrix rightAnswer, your code triggers context transition for each month, so you alwasy see the months in the visual. Please change the code according to:
calculate_new_subs =
VAR selection =[month_selection]
RETURNCALCULATE(sum(net_subscriber_growth[NewSubscribers]),FILTER(net_subscriber_growth,MONTH(net_subscriber_growth[Month]) = selection ))
--------------------------------
2) Second question would be if i am sum I am able to achieve the same reuslt as above.
What is the diff between usingdax1)
month_selection = SELECTEDVALUE('calendar'[month_no],0)
dax2)calculate_new_subs = CALCULATE(sum(net_subscriber_growth[NewSubscribers]),FILTER(net_subscriber_growth,MONTH(net_subscriber_growth[Month]) = [month_selection]))
andSum_new_subscriber =SUM(net_subscriber_growth[NewSubscribers])
if end result is same only in this scenario?Answer: see first question, you were triggering context transition so you alwasy see the current month and so CALCULATE is useless with that code (see my version to obtain what you want)-----------------------------------------------------------------------------------------
3) if i am using all inside calculate filter.calculate_new_subs = CALCULATE(sum(net_subscriber_growth[NewSubscribers]),FILTER(all(net_subscriber_growth),MONTH(net_subscriber_growth[Month]) = [month_selection]))
I am getting result as below.
Is this the expected behavior of ALL Dax?Answer: yes you are considering all the table for every month with that code, and you are still triggering context transition so you see all months for every month now
----------------------------------------------------------------------------------
4) The fourth quetsion would be what is the diff between
1)CALCULATE(sum(net_subscriber_growth[NewSubscribers]),FILTER(net_subscriber_growth,MONTH(net_subscriber_growth[Month]) = [month_selection]))
and
2) CALCULATE(sum(net_subscriber_growth[NewSubscribers]),MONTH(net_subscriber_growth[Month]) = [month_selection])Answer
2) is automatically translated into
CALCULATE(
sum(net_subscriber_growth[NewSubscribers]),
FILTER (
ALL ( net_subscriber_growth[Month]) ),
net_subscriber_growth[Month] = [month_selection]
)
)so in case 2 you are only applying the condition to the month column, ignoring filters if they exist (like the month selected), in the first case you are applying the condition to each row of the entire table and you are considering the filters applied to that table (example the month)
The 2) case is more efficient
---------------------------------------------If this helped, please consider giving kudos and mark as a solution
me in replies or I'll lose your thread
consider voting this Power BI idea
Francesco Bergamaschi
MBA, M.Eng, M.Econ, Professor of BI
Answers are below, based on the the below code
dax1)
dax2)
1)The first question would be why i am getting calcaulate_new_subs value for all month insite i have not clicked anything in the slicer.
Ideally 0 should have been supplied according to the dax and blank should be returned in the matrix right
Answer, your code triggers context transition for each month, so you alwasy see the months in the visual. Please change the code according to:
calculate_new_subs =
VAR selection =[month_selection]
RETURN
CALCULATE(sum(net_subscriber_growth[NewSubscribers]),FILTER(net_subscriber_growth,MONTH(net_subscriber_growth[Month]) = selection ))
--------------------------------
2) Second question would be if i am sum I am able to achieve the same reuslt as above.
What is the diff between using
dax1)
dax2)
and
if end result is same only in this scenario?
-----------------------------------------------------------------------------------------
3) if i am using all inside calculate filter.
calculate_new_subs = CALCULATE(sum(net_subscriber_growth[NewSubscribers]),FILTER(all(net_subscriber_growth),MONTH(net_subscriber_growth[Month]) = [month_selection]))
I am getting result as below.
Is this the expected behavior of ALL Dax?
Answer: yes you are considering all the table for every month with that code, and you are still triggering context transition so you see all months for every month now
----------------------------------------------------------------------------------
4) The fourth quetsion would be what is the diff between
1)CALCULATE(sum(net_subscriber_growth[NewSubscribers]),FILTER(net_subscriber_growth,MONTH(net_subscriber_growth[Month]) = [month_selection]))
and
2) CALCULATE(sum(net_subscriber_growth[NewSubscribers]),MONTH(net_subscriber_growth[Month]) = [month_selection])
Answer
2) is automatically translated into
CALCULATE(
sum(net_subscriber_growth[NewSubscribers]),
FILTER (
ALL ( net_subscriber_growth[Month]) ),
net_subscriber_growth[Month] = [month_selection]
)
)
so in case 2 you are only applying the condition to the month column, ignoring filters if they exist (like the month selected), in the first case you are applying the condition to each row of the entire table and you are considering the filters applied to that table (example the month)
The 2) case is more efficient
---------------------------------------------
If this helped, please consider giving kudos and mark as a solution
me in replies or I'll lose your thread
consider voting this Power BI idea
Francesco Bergamaschi
MBA, M.Eng, M.Econ, Professor of BI
- maverickf171 year agoHelper I
Hey FBergamaschi can we connect please over a gmeet?
If it is fine?
Please DM me.