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

Prepping for a Fabric certification exam? Join us for a live prep session with exam experts to learn how to pass the exam. Register now.

Reply
JBennett12
Regular Visitor

I need help understanding this dax code and the functions used in it. Calculate, filter.

I was tasked to make some python visualizations with this dax code: 

Inclusion_Expected = 
    VAR _item_number = Source_With_Trace_ID[Item_Number]
    VAR _cast_date = Source_With_Trace_ID[Casting_Date]
    VAR _start_date = _cast_date - 365
    VAR _inclusions = 
        CALCULATE(
            SUM(Source_With_Trace_ID[Inclusion]),
            FILTER(Source_With_Trace_ID,
                    Source_With_Trace_ID[Item_Number] = _item_number
                    && Source_With_Trace_ID[Casting_Date] >= _start_date
                    && Source_With_Trace_ID[Casting_Date] < _cast_date
            )
        )

    VAR _closed = 
        CALCULATE(
            SUM(Source_With_Trace_ID[Closed]),
            FILTER(Source_With_Trace_ID,
            Source_With_Trace_ID[Item_Number] = _item_number
            && Source_With_Trace_ID[Casting_Date] >= _start_date
            && Source_With_Trace_ID[Casting_Date] < _cast_date
            )
        )

    VAR _inclusion_scrap_rate = DIVIDE(_inclusions, _closed)

    RETURN _inclusion_scrap_rate

I think I understand it, but when trying to translate it over I'm getting different results.

First in the filter statement, does 

Source_With_Trace_ID[Casting_Date] < _cast_date actually do anything? From my understanding, because I set a variable up top to equal that column, it should always be false.
Second, from my understanding, calculate allows me to do an aggregation on a column, while using the filter statement to filter my table however I like it?
 
Third, I don't understand how each of my rows have a different value in the calculated column instead of a single value throughout the entire column. Because of the aggregation, I assumed they would all be the same for 
_inclusion_scrap_rate.
 
Thank you for any help provided!
1 REPLY 1
Greg_Deckler
Super User
Super User

@JBennett12 It is doing something because of how CALCULATE replaces filtering context and the fact that you are operating essentially in the context of ALL when you are doing those CALCULATE statements. The variables at the beginning are just preserving the row context that you want to use later. You can think of the first CALCULATE statement like the following:

SUMX(
  FILTER(
    ALL('Source_With_Trace_ID'),
    [Item_Number] = _item_number
    && [Casting_Date] >= _start_date
    && [Casting_Date] < _cast_date
  ),
  [Inclusion]
)


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
PBIApril_Carousel

Power BI Monthly Update - April 2025

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

Notebook Gallery Carousel1

NEW! Community Notebooks Gallery

Explore and share Fabric Notebooks to boost Power BI insights in the new community notebooks gallery.

April2025 Carousel

Fabric Community Update - April 2025

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