Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Distinct count on measure within table context

Hi all,

 

Frustration is at an all-time high today..

 

I have a very complex model in my PowerBI. Lots of tables and measures.

 

In the end, I have created this table using a complex measure:

 

Calendar[DateTime]Calendar[Complex Measure]
1-1-2022 16:001-1-2022: FLAG
1-1-2022 17:001-1-2022: FLAG
1-1-2022 18:001-1-2022: FLAG
2-1-2022 9:002-1-2022: FLAG
2-1-2022 12:002-1-2022: FLAG

 

I want to count the distinct values in Dimension[Complex Measure] and present the result in a card-visual. In this case this should be '2'.


The table above only looks like this when I include both [DateTime] & [Complex Measure].

 

[Complex Measure] contains other measures from other tables that depend on the Calendar[DateTime]-context. It basically looks like:

 

 

Complex Measure = IF(Table1[Measure X] - Table2[Measure Y] < 0, MIN(Calender[DateTime]) & ": FLAG", BLANK())

 

 

How do I create a new measure that performs a correct distinct count on Calendar[Complex Measure] so I can use that in a card-visual?

  • Anonymous's avatar
    Anonymous
    2 years ago

    Okay, I somehow managed to create a working measure.

    It looks like this:

     

     

    CALCULATE(
        COUNTROWS(SUMMARIZE(Calender, Calender[Date])) + 0, 
             FILTER(Calender, [Number of objects available & unavailable with valid reason] - [Minimal number of objects] < 0)
            )

     


    I completely skipped the part where I create a measure that contains the word 'FLAG' and then performing a (distinct) count on that measure.  I think the problem was that in my 'FLAG'-measure, I used:

    MIN(Calender[DateTime])

     
    .. which is why it only worked when I included the [DateTime].

12 Replies

  • AllisonKennedy's avatar
    AllisonKennedy
    Community Champion

    Anonymous  See if this works for you: 

     

    Distinct Count Complex Measure =
    VAR _TableContext = ADDCOLUMNS( Calendar, "Row Context", Calendar[DateTime], "Complex Measure", [Complex Measure])

    VAR _TableIndex = SUMMARIZE(_TableContext, [Complex Measure] )

    VAR _Result = COUNTROWS( _TableIndex)
    RETURN _Result
    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi AllisonKennedy,

       

      Thanks for your response.

      It seems that your measure is not giving me the desired result. My best guess is that you're not taking 'distinct' values but that you're counting all rows that have a result for [Complex Measure]? 

      • AllisonKennedy's avatar
        AllisonKennedy
        Community Champion

        Anonymous Can you provide screenshots of what you've done and it not working? The SUMMARIZE function should group it by the value of the complex measure column we created in the first variable. I've renamed the virtual column I created to make it a little less ambiguous: 

         

        Distinct Count Complex Measure =
        VAR _TableContext = ADDCOLUMNS( 'Calendar', "Row Context", 'Calendar'[DateTime], "Complex Measure In Context", [Complex Measure])

        VAR _TableIndex = SUMMARIZE(_TableContext, [Complex Measure In Context] )

        VAR _Result = COUNTROWS( _TableIndex)
        RETURN _Result