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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

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
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors