Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
tvaishnav
Helper IV
Helper IV

DAX : Can anyone help me understand the difference between these two DAX codes that I have written?

I have written these two set of code. Can anyone tell me in plain English how are these two evaluated. Second version gives me the desired answer. My understanding of second one is as follows:

SUMX opens the row context over the table produced as a output from FILTER. SUMX then evaluates VAL over each row the output table. CALCULATE removes date filter so that SUMX evaluates for the all dates available.

I am confused why first one isn't giving the same output? First DAX code evaluates as if there is not Removefilter being applied.

 

AP = CALCULATE(
                SUM('Net Cash Position'[Val])
                ,REMOVEFILTERS('Date')
                ,FILTER('Net Cash Position'
                        ,'Net Cash Position'[Metric] = "Accounts Payable"
                        )
                )
AP = CALCULATE(
                SUMX(
                    FILTER(
                        'Net Cash Position'
                        ,'Net Cash Position'[Metric] = "Accounts Payable"
                        )
                     ,'Net Cash Position'[Val]
                    )
                ,REMOVEFILTERS('Date')
                )

 

 

1 ACCEPTED SOLUTION
SpartaBI
Community Champion
Community Champion

@tvaishnav in the first one the filter:

                ,FILTER('Net Cash Position'
                        ,'Net Cash Position'[Metric] = "Accounts Payable"
                        )


Is evaluated before / regardless of the removefilters on the date while in the 2nd one it's evaluated after the removefilers on date applied.
Because you are calling the entire table in the filter it calls the entire expanded table and you will get different result in the 2 scenarios.
Try writing the 1st one like this:

AP = CALCULATE(
                SUM('Net Cash Position'[Val])
                ,REMOVEFILTERS('Date')
                ,'Net Cash Position'[Metric] = "Accounts Payable"
     )

Also let me know if this also gives you the right anser:

AP = CALCULATE(
                SUM('Net Cash Position'[Val])
                ,REMOVEFILTERS('Date')
                ,FILTER(VALUES('Net Cash Position'[Metric]),
                        ,'Net Cash Position'[Metric] = "Accounts Payable"
                        )
                )





2022-05-19 17_30_22-Re_ Need help on DAX function with measure vs colu... - Microsoft Power BI Commu.png


Showcase Report – Contoso By SpartaBI

Full-Logo11.png

SpartaBI_3-1652115470761.png   SpartaBI_1-1652115142093.png   SpartaBI_2-1652115154505.png

View solution in original post

6 REPLIES 6
johnt75
Super User
Super User

In the first example REMOVEFILTERS is not being applied to the FILTER function - both it and FILTER are being applied only to the SUM function.

In the second example, because FILTER is inside the SUMX it is picking up the context which includes the REMOVEFILTERS.

In a CALCULATE function, any filters which are applied at the same level, like in the first example, are not applied to one another, they are merely added to the filter context which is used for the main calculation.

Thank you for the response. That explanation does help but I have a follow up question regarding code sample 1.

 


@johnt75 wrote:

In the first example REMOVEFILTERS is not being applied to the FILTER function - both it and FILTER are being applied only to the SUM function.

In the second example, because FILTER is inside the SUMX it is picking up the context which includes the REMOVEFILTERS.

In a CALCULATE function, any filters which are applied at the same level, like in the first example, are not applied to one another, they are merely added to the filter context which is used for the main calculation.


Keeping this aspect of CALCULATE function in mind, in code sample 1, REMOVEFILTER will remove date filter and FILTER will obtain rows over which I want to do the summation. I know this interpretation of code is wrong. Can you tell me what is wrong here?

FILTER will retrieve the rows matching "Accounts Payable" with the existing filter context in place, so any filters on the date table will also be active. It will therefore only return rows which match Accounts Payable and which match the filters on the date table.

When REMOVEFILTERS is then called, there's really nothing for it do as all the date filters have already been applied

SpartaBI
Community Champion
Community Champion

@tvaishnav in the first one the filter:

                ,FILTER('Net Cash Position'
                        ,'Net Cash Position'[Metric] = "Accounts Payable"
                        )


Is evaluated before / regardless of the removefilters on the date while in the 2nd one it's evaluated after the removefilers on date applied.
Because you are calling the entire table in the filter it calls the entire expanded table and you will get different result in the 2 scenarios.
Try writing the 1st one like this:

AP = CALCULATE(
                SUM('Net Cash Position'[Val])
                ,REMOVEFILTERS('Date')
                ,'Net Cash Position'[Metric] = "Accounts Payable"
     )

Also let me know if this also gives you the right anser:

AP = CALCULATE(
                SUM('Net Cash Position'[Val])
                ,REMOVEFILTERS('Date')
                ,FILTER(VALUES('Net Cash Position'[Metric]),
                        ,'Net Cash Position'[Metric] = "Accounts Payable"
                        )
                )





2022-05-19 17_30_22-Re_ Need help on DAX function with measure vs colu... - Microsoft Power BI Commu.png


Showcase Report – Contoso By SpartaBI

Full-Logo11.png

SpartaBI_3-1652115470761.png   SpartaBI_1-1652115142093.png   SpartaBI_2-1652115154505.png

@SpartaBI I will test this out and get back. I do have a follow up question but I want to play with your code first. Thank you for your time.

@tvaishnav sure.

In case this one was solved please don't forget to accept my previuos message as a solution

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.