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
Dario729
Frequent Visitor

Create a new table from another and add new calculations from rows to columns

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:

 

Sin título.png

 

Thanks in advance

 

1 ACCEPTED SOLUTION
tamerj1
Super User
Super User

@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 )
)

View solution in original post

2 REPLIES 2
tamerj1
Super User
Super User

@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 )
)
Greg_Deckler
Super User
Super User

@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.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

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.

Top Solution Authors