Forum Discussion
jarivera777
2 years agoFrequent Visitor
Counting multiple duplicate columns by a distinct column.
Hi all, I am new to Power BI and I need a solution. I have 5 columns (Impacts 1-5) that have duplicates and blanks, but the primary key (ID) is distinct: I need to create a distinct column of ...
jgeddes
2 years agoSuper User
To expand upon bhanu_gautam 's answer, you can do the whole transformation in Power Query.
Here is an example code you can paste into the advanced editor of a blank query so you can review each step needed.
The end result will look something like this...
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bZG9DoMwDITfJTMojfkLa/eqpVuFGKI2YmgEFSri9ZsY4qCGBc6E+2xf2pYJljDgksMJcisbkW7qrl/0vE5q6LUVZzPjaxqXgXVJy8BWBS/JDt6+/UnOFfTQxoyLFWjOrBC8invf5uljon6uVM93wDhGjgPIeICL6vXwVfum4ZMHIqKwQq4IN1GTpZsio+9L24QTSqK0Vc2hiDF/GYQp6AABFW4iBAEgioPEwXLIkC5RWO8DGXm0y/5eD9aoHUHwOib4zGjqOA3WdT8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, #"Closed Date" = _t, #"Qtr/Yr" = _t, #"Impact 1" = _t, #"Impact 2" = _t, #"Impact 3" = _t, #"Impact 4" = _t, #"Impact 5" = _t]),
#"Changed Type" =
Table.TransformColumnTypes(
Source,
{
{"ID", Int64.Type}, {"Closed Date", type date}, {"Qtr/Yr", type text}, {"Impact 1", type text}, {"Impact 2", type text}, {"Impact 3", type text}, {"Impact 4", type text}, {"Impact 5", type text}
}
),
distinctQuarters =
List.Distinct(#"Changed Type"[#"Qtr/Yr"]),
#"Removed Columns" =
Table.RemoveColumns(
#"Changed Type",
{"ID", "Closed Date"}
),
#"Unpivoted Other Columns" =
Table.UnpivotOtherColumns(
#"Removed Columns",
{"Qtr/Yr"},
"Attribute",
"Value"
),
#"Grouped Rows" =
Table.Group(
#"Unpivoted Other Columns",
{"Value"},
{
{"Total", each Table.RowCount(_), Int64.Type}, {"nestedTable", each Table.SelectColumns(_, "Qtr/Yr"), type table [#"Qtr/Yr"=nullable text, Value=text]}
}
),
groupNested =
Table.TransformColumns(
#"Grouped Rows",
{
{"nestedTable", each Table.Group(_, {"Qtr/Yr"}, {{"qtrCount", each Table.RowCount(_), Int64.Type}})}
}
),
pivotNested =
Table.TransformColumns(
groupNested,
{
{"nestedTable", each Table.Pivot(_, List.Distinct(_[#"Qtr/Yr"]), "Qtr/Yr", "qtrCount")}
}
),
expandNested =
Table.ExpandTableColumn(
pivotNested,
"nestedTable",
distinctQuarters
),
removeBlankColors =
Table.SelectRows(
expandNested,
each ([Value] <> "")
),
changeQtrTypes =
Table.TransformColumnTypes(
removeBlankColors,
{
{"Q1-2024", Int64.Type}, {"Q2-2024", Int64.Type}, {"Q3-2023", Int64.Type}, {"Q4-2023", Int64.Type}
}
)
in
changeQtrTypesjarivera777
2 years agoFrequent Visitor
Hi There! After I "Removed Duplicates", I noticed the Attribute column only captured Color Impacts 1 & 2, and, in the Value column, it did not capture all of my colors. When I selected "Closed and Apply", I recieved the following error notification: