Power BI is turning 10! Tune in for a special live episode on July 24 with behind-the-scenes stories, product evolution highlights, and a sneak peek at what’s in store for the future.
Save the dateEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
Hi, I need to create a new set of measures, but they depend of other values in a preexisting source of data. In SQL I managed this by adding CASE sentences to transform a row into a column and then I operated the columns, but in DAX I'm not sure about what is the best option, so I'm guessing that is through a new table with the SWITCH function involved. Let me explain my need:
Thanks in advance
Solved! Go to Solution.
@Hi @Dario729
please try
Desired =
GENERATE (
VALUES ( Source[Customers] ),
VAR Label1 =
CALCULATE ( SUM ( Source[Inputs] ), Source[Labels] = 1 )
VAR Label2 =
CALCULATE ( SUM ( Source[Inputs] ), Source[Labels] = 2 )
VAR Outputs = Label1 - Label2
RETURN
ROW ( "Label1", Label1, "Label2", Label2, "Outputs", Outputs )
)
@Hi @Dario729
please try
Desired =
GENERATE (
VALUES ( Source[Customers] ),
VAR Label1 =
CALCULATE ( SUM ( Source[Inputs] ), Source[Labels] = 1 )
VAR Label2 =
CALCULATE ( SUM ( Source[Inputs] ), Source[Labels] = 2 )
VAR Outputs = Label1 - Label2
RETURN
ROW ( "Label1", Label1, "Label2", Label2, "Outputs", Outputs )
)
@Dario729 You could do this in PQ:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcg41VNJRAmMDA6VYHZiIEQjDRYygasxQREDYBC5iDFVjChKJBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Customers = _t, Labels = _t, Inputs = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Customers", type text}, {"Labels", Int64.Type}, {"Inputs", Int64.Type}}),
#"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Changed Type", {{"Labels", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Changed Type", {{"Labels", type text}}, "en-US")[Labels]), "Labels", "Inputs", List.Sum),
#"Renamed Columns" = Table.RenameColumns(#"Pivoted Column",{{"1", "Label1"}, {"2", "Label2"}}),
#"Replaced Value" = Table.ReplaceValue(#"Renamed Columns",null,0,Replacer.ReplaceValue,{"Label2"}),
#"Added Custom" = Table.AddColumn(#"Replaced Value", "Output", each [Label1] - [Label2])
in
#"Added Custom"
and here is a DAX solution as well:
Table3 =
ADDCOLUMNS(
ADDCOLUMNS(
DISTINCT('Table'[Customers]),
"Label1", SUMX(FILTER('Table',[Labels] = 1 && [Customers] = EARLIER([Customers])),[Inputs]),
"Label2", SUMX(FILTER('Table',[Labels] = 2 && [Customers] = EARLIER([Customers])),[Inputs])
),
"Output",[Label1] - [Label2]
)
PBIX is attached below signature.
User | Count |
---|---|
25 | |
12 | |
8 | |
6 | |
6 |
User | Count |
---|---|
26 | |
12 | |
11 | |
9 | |
6 |