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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Combine Several Columns Fields to List and Count Distinct

Let me start by describing the final output. I have values in column A, B, C, D. I need to determine the if there are duplicates in A2, B2, C2, D2.

 

I've searched around since the morning and every "list" function always lists the entire column. I don't want the entire column as list. I want a list with values A2, B2, C2, and D2. From there, I'm guessing I would have to group and then count the output. I can't seem to figure out how to send the date values in the four columns to a list and then select, List.Distinct.

 

image.png

2 ACCEPTED SOLUTIONS
Anonymous
Not applicable

In Power Query:

Select ID Column --> Unpivot Other Columns

Remove Attribute

GroupBy ID and Count Disticnt Rows

 

PQ Dist Count.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjU1VdJRMtY3AiIDQ0s0tqm+oQGMbaZvaAphx+pEK5mbmwPFTPRNYdLYmRb6llA9sQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [ID = _t, Date1 = _t, Date2 = _t, Date3 = _t, Date4 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Date1", type date}, {"Date2", type date}, {"Date3", type date}, {"Date4", type date}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"ID"}, "Attribute", "Value"),
    #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Other Columns",{"Attribute"}),
    #"Grouped Rows" = Table.Group(#"Removed Columns", {"ID"}, {{"Distcin Count", each Table.RowCount(Table.Distinct(_)), type number}})
in
    #"Grouped Rows"

View solution in original post

Nolock
Resident Rockstar
Resident Rockstar

Hi @Anonymous,

 

there is also another solution. You can create a table of cells of a row (each Table.FromRecords({[Dt = [Date1]], [Dt = [Date2]], [Dt = [Date3]]})), then apply Distict and RowCount.

The example contains also some test data (3 columns Date1, Date2, and Date3):

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQz1DMyMLRU0lEy0jOCMY31jCHMWJ1oJRM9E5i4qZ4pBhOkxEzPDCaOxDTXM4cqiQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Date1 = _t, Date2 = _t, Date3 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date1", type date}, {"Date2", type date}, {"Date3", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "AllDatesAsTable", each Table.FromRecords({[Dt = [Date1]], [Dt = [Date2]], [Dt = [Date3]]})),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "DistinctDatesAsTable", each Table.Distinct([AllDatesAsTable], "Dt")),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "DistinctCount", each Table.RowCount([DistinctDatesAsTable]))
in
    #"Added Custom2"

View solution in original post

2 REPLIES 2
Nolock
Resident Rockstar
Resident Rockstar

Hi @Anonymous,

 

there is also another solution. You can create a table of cells of a row (each Table.FromRecords({[Dt = [Date1]], [Dt = [Date2]], [Dt = [Date3]]})), then apply Distict and RowCount.

The example contains also some test data (3 columns Date1, Date2, and Date3):

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQz1DMyMLRU0lEy0jOCMY31jCHMWJ1oJRM9E5i4qZ4pBhOkxEzPDCaOxDTXM4cqiQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Date1 = _t, Date2 = _t, Date3 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date1", type date}, {"Date2", type date}, {"Date3", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "AllDatesAsTable", each Table.FromRecords({[Dt = [Date1]], [Dt = [Date2]], [Dt = [Date3]]})),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "DistinctDatesAsTable", each Table.Distinct([AllDatesAsTable], "Dt")),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "DistinctCount", each Table.RowCount([DistinctDatesAsTable]))
in
    #"Added Custom2"
Anonymous
Not applicable

In Power Query:

Select ID Column --> Unpivot Other Columns

Remove Attribute

GroupBy ID and Count Disticnt Rows

 

PQ Dist Count.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjU1VdJRMtY3AiIDQ0s0tqm+oQGMbaZvaAphx+pEK5mbmwPFTPRNYdLYmRb6llA9sQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [ID = _t, Date1 = _t, Date2 = _t, Date3 = _t, Date4 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Date1", type date}, {"Date2", type date}, {"Date3", type date}, {"Date4", type date}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"ID"}, "Attribute", "Value"),
    #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Other Columns",{"Attribute"}),
    #"Grouped Rows" = Table.Group(#"Removed Columns", {"ID"}, {{"Distcin Count", each Table.RowCount(Table.Distinct(_)), type number}})
in
    #"Grouped Rows"

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.