Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago

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

  • Greg_Deckler's avatar
    Greg_Deckler
    Icon for Community Champion rankCommunity Champion

    Anonymous 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]
    )