cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Mann
Resolver III
Resolver III

DistinctCount of a column with GroupBy from a Temporary DAX Table in a Variable

Hi Guys,

 

I created a DAX query which is returning a table like this:

ClassTypeIDs
AX11
AX12
BX13
BX13
CY15
CY15
CY16
DY11

 

For example created Datatable in DAX for easy understanding:

 

Test = 
VAR Source=
DATATABLE("Class",STRING,"Type",STRING,"IDs",INTEGER,
            {{"A","X",11},{"A","X",12},{"B","X",13},{"B","X",13},{"C","Y",15},{"C","Y",15},{"C","Y",16},{"D","Y",11}})
Return
Source

 

I need to find the Distinct count of columns "IDs" as per grouping of "Class" and "Type" using Variable Table "Source". How can I do this in DAX to give me something like below result:

ClassTypeIDs
AX2
BX1
CY2
DY1

 

 

Thanks.

1 ACCEPTED SOLUTION
v-lid-msft
Community Support
Community Support

Hi @Mann ,

 

We can create a calculated table using following formula to meet your requirement:

 

Test 2 =
VAR Source =
    DATATABLE (
        "Class", STRING,
        "Type", STRING,
        "IDs", INTEGER,
        {
            { "A", "X", 11 },
            { "A", "X", 12 },
            { "B", "X", 13 },
            { "B", "X", 13 },
            { "C", "Y", 15 },
            { "C", "Y", 15 },
            { "C", "Y", 16 },
            { "D", "Y", 11 }
        }
    )
RETURN
    ADDCOLUMNS (
        DISTINCT ( SELECTCOLUMNS ( Source, "Class", [Class], "Type", [Type] ) ),
        "IDs",
        VAR c = [Class]
        VAR t = [Type]
        RETURN
            COUNTROWS ( DISTINCT ( FILTER ( Source, [Type] = t && [Class] = c ) ) )
    )

 

 

9.PNG


BTW, pbix as attached.

 

Best regards,

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
parry2k
Super User
Super User

@Mann add a calculated measure with following expression, change table and column name as per your data model

 

Distinct ID Count = DISTINCTCOUNT ( 'Table (3)'[IDs] )

 






Did I answer your question? Mark my post as a solution.

Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.





Hi @parry2k 

 

I can't add this as a measure since I need to return the expected table which will be used as a dataset in my paginated report. I have to return this expected output as a result of same code.

I tried SUMMARIZE or SUMMARIZECOLUMNS or GROUPBY but none of these helping out.

 

Mann

v-lid-msft
Community Support
Community Support

Hi @Mann ,

 

We can create a calculated table using following formula to meet your requirement:

 

Test 2 =
VAR Source =
    DATATABLE (
        "Class", STRING,
        "Type", STRING,
        "IDs", INTEGER,
        {
            { "A", "X", 11 },
            { "A", "X", 12 },
            { "B", "X", 13 },
            { "B", "X", 13 },
            { "C", "Y", 15 },
            { "C", "Y", 15 },
            { "C", "Y", 16 },
            { "D", "Y", 11 }
        }
    )
RETURN
    ADDCOLUMNS (
        DISTINCT ( SELECTCOLUMNS ( Source, "Class", [Class], "Type", [Type] ) ),
        "IDs",
        VAR c = [Class]
        VAR t = [Type]
        RETURN
            COUNTROWS ( DISTINCT ( FILTER ( Source, [Type] = t && [Class] = c ) ) )
    )

 

 

9.PNG


BTW, pbix as attached.

 

Best regards,

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

Thanks @v-lid-msft 

 

It worked as expected. 😊

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

November Power BI Update 768x460.png

Check it Out!

Click here to read more about the November 2021 Updates!

M365 768x460.jpg

Microsoft 365 Collaboration Conference | December 7–9, 2021

Join us, in-person, December 7–9 in Las Vegas, for the largest gathering of the Microsoft community in the world.