March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early bird discount ends December 31.
Register NowBe one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now
Scenario:
Suppose I would like to
for each machine. How can I achieve this requirement?
Sample Data:
Method1: In DAX
DAX Operation:
Table 2 =
VAR _t =
SUMMARIZECOLUMNS ( 'Table'[Machine Name], 'Table'[Event] )
VAR _t1 =
DISTINCT (
SELECTCOLUMNS (
_t,
"Machine Name", [Machine Name],
"Event",
"Fail" & ": "
& COUNTROWS (
FILTER (
'Table',
'Table'[Event] = "Fail"
&& 'Table'[Machine Name] = EARLIER ( 'Table'[Machine Name] )
)
)
)
)
VAR _t2 =
DISTINCT (
SELECTCOLUMNS (
ADDCOLUMNS (
_t,
"a",
"Failure Reasons" & ": "
& CONCATENATEX (
FILTER (
_t,
[Event] <> "Fail"
&& [Event] <> "Success"
&& [Machine Name] = EARLIER ( [Machine Name] )
),
[Event],
" // "
)
),
"Machine Name", [Machine Name],
"Event", [a]
)
)
VAR _t3 =
SUMMARIZE (
'Table',
'Table'[Machine Name],
"Event",
"Success" & ": "
& CALCULATE (
COUNT ( 'Table'[Event] ),
FILTER (
'Table',
'Table'[Event] = "Success"
&& 'Table'[Machine Name] = EARLIER ( 'Table'[Machine Name] )
)
)
)
RETURN
UNION ( _t1, _t2, _t3 )
Output:
Method2: In Power Query
1. Duplicate Table named Fail
2. Duplicate Table named Fail Reason
3. Duplicate Table named Success
Almost the same as step1
4. Append these three tables
M Operation in Advanced Editor:
Fail table:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZC9CsIwFEZfJWRO4db8mLjd0IhgHWybxdJBioPgVnx/U6VQSEjNcCFwvu+epO8pB6604pRRmE8lre5Ahuvx/nzRgS2EiIn2PY6PaVpBcrMmscgXXCELUyM5EOutrV2+M58wsCQEP2kBu8jCqE0i0eEL0MiIL0rAfFdA+OxXiq8fVqTGm2vIBZvzKqnjZPSlZv8PZDbfkyCCn8SfXuOu3rUdHYYP", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, #"Machine Name" = _t, Event = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Machine Name", type text}, {"Event", type text}}),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"ID"}),
#"Filtered Rows" = Table.SelectRows(#"Removed Columns", each [Event] = "Fail"),
#"Grouped Rows" = Table.Group(#"Filtered Rows", {"Machine Name"}, {{"Count", each Table.RowCount(_), Int64.Type}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Event", each "Fail" &": "&Number.ToText([Count])),
#"Removed Columns1" = Table.RemoveColumns(#"Added Custom",{"Count"})
in
#"Removed Columns1"
Fail Reasons table:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZC9CsIwFEZfJWRO4db8mLjd0IhgHWybxdJBioPgVnx/U6VQSEjNcCFwvu+epO8pB6604pRRmE8lre5Ahuvx/nzRgS2EiIn2PY6PaVpBcrMmscgXXCELUyM5EOutrV2+M58wsCQEP2kBu8jCqE0i0eEL0MiIL0rAfFdA+OxXiq8fVqTGm2vIBZvzKqnjZPSlZv8PZDbfkyCCn8SfXuOu3rUdHYYP", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, #"Machine Name" = _t, Event = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Machine Name", type text}, {"Event", type text}}),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"ID"}),
#"Filtered Rows" = Table.SelectRows(#"Removed Columns", each [Event] <> "Fail" and [Event] <> "Success")
, #"Grouped Rows" = Table.Group( #"Filtered Rows", {"Machine Name"}, {{"Event", each Text.Combine(List.Distinct([Event]), " // "), type text}})
in
#"Grouped Rows"
Success table
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZC9CsIwFEZfJWRO4db8mLjd0IhgHWybxdJBioPgVnx/U6VQSEjNcCFwvu+epO8pB6604pRRmE8lre5Ahuvx/nzRgS2EiIn2PY6PaVpBcrMmscgXXCELUyM5EOutrV2+M58wsCQEP2kBu8jCqE0i0eEL0MiIL0rAfFdA+OxXiq8fVqTGm2vIBZvzKqnjZPSlZv8PZDbfkyCCn8SfXuOu3rUdHYYP", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, #"Machine Name" = _t, Event = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Machine Name", type text}, {"Event", type text}}),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"ID"}),
#"Filtered Rows" = Table.SelectRows(#"Removed Columns", each [Event] = "Success"),
#"Grouped Rows" = Table.Group(#"Filtered Rows", {"Machine Name"}, {{"Count", each Table.RowCount(_), Int64.Type}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Event", each "Success"&": "&Number.ToText([Count])),
#"Removed Columns1" = Table.RemoveColumns(#"Added Custom",{"Count"})
in
#"Removed Columns1"
Output:
Note:
In this method, since I created three tables by duplicating, it will memory/CPU to increase somewhat. And normally, memory consumption will reduce the performance and brings more load to the model.
So it is recommended to use Method1.
Please check the attached file for details.
I hope this article can help you with the similar question.
Author: Eyelyn Qin
Reviewer: Icey Zhang & Lin Li
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.