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

Level up your Power BI skills this month - build one visual each week and tell better stories with data! Get started

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
Community Champion
Community Champion

@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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

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

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.