cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
null
Helper V
Helper V

unpivot the data and also consider context use DAX

Hi, I want to create a combo visual and so the data needed is one common axis column and two values,

the data seems need to be unpivoted and there are many others fiters to be considered, like location and other columns not listed.

I am new user of DAX and ask for help,  could this problem can be solved use DAX?  Thank you.

null_0-1655446373228.png

1 ACCEPTED SOLUTION
v-yangliu-msft
Community Support
Community Support

Hi  @null ,

Here are the steps you can follow:

1. Use Enter data to create a table.

vyangliumsft_0-1655864537480.png

2. Select the following columns in the power query and use Unpivot Column.

vyangliumsft_1-1655864537484.png

3. Create calculated column.

Table

left =
LEFT('Table'[Attribute],3)
right = RIGHT('Table'[Attribute],1)

vyangliumsft_2-1655864537484.png

Table2:

Right =
RIGHT('Table2'[Month],1)

vyangliumsft_3-1655864537484.png

4. Create measure.

Bar_measure =
CALCULATE(SUM('Table'[Value]),FILTER(ALL('Table'),'Table'[left]="bar"&&'Table'[right]=MAX('Table2'[Right])))
Lin_measure =
CALCULATE(SUM('Table'[Value]),FILTER(ALL('Table'),'Table'[left]="lin"&&'Table'[right]=MAX('Table2'[Right])))

5. Result:

vyangliumsft_4-1655864537487.png

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

View solution in original post

4 REPLIES 4
v-yangliu-msft
Community Support
Community Support

Hi  @null ,

Here are the steps you can follow:

1. Use Enter data to create a table.

vyangliumsft_0-1655864537480.png

2. Select the following columns in the power query and use Unpivot Column.

vyangliumsft_1-1655864537484.png

3. Create calculated column.

Table

left =
LEFT('Table'[Attribute],3)
right = RIGHT('Table'[Attribute],1)

vyangliumsft_2-1655864537484.png

Table2:

Right =
RIGHT('Table2'[Month],1)

vyangliumsft_3-1655864537484.png

4. Create measure.

Bar_measure =
CALCULATE(SUM('Table'[Value]),FILTER(ALL('Table'),'Table'[left]="bar"&&'Table'[right]=MAX('Table2'[Right])))
Lin_measure =
CALCULATE(SUM('Table'[Value]),FILTER(ALL('Table'),'Table'[left]="lin"&&'Table'[right]=MAX('Table2'[Right])))

5. Result:

vyangliumsft_4-1655864537487.png

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

NickolajJessen
Solution Sage
Solution Sage

Throw this into your advanced editor

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSUTIEYmMoNleK1YlWSoLyjKDYFCyaDFJriCRsDBZOAbLMkIwAKomNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [location = _t, bar = _t, bar2 = _t, line = _t, line2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"location", type text}, {"bar", Int64.Type}, {"bar2", Int64.Type}, {"line", Int64.Type}, {"line2", Int64.Type}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"location"}, "Attribute", "Value"),
    #"Inserted First Characters" = Table.AddColumn(#"Unpivoted Other Columns", "First Characters", each Text.Start([Attribute], 3), type text),
    #"Replaced Value" = Table.ReplaceValue(#"Inserted First Characters","bar","Jan",Replacer.ReplaceValue,{"Attribute", "Value"}),
    #"Replaced Value1" = Table.ReplaceValue(#"Replaced Value","bar2","feb",Replacer.ReplaceValue,{"Attribute", "Value"}),
    #"Replaced Value2" = Table.ReplaceValue(#"Replaced Value1","line","jan",Replacer.ReplaceValue,{"Attribute", "Value"}),
    #"Replaced Value3" = Table.ReplaceValue(#"Replaced Value2","line2","feb",Replacer.ReplaceValue,{"Attribute", "Value"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Replaced Value3",{{"Value", Int64.Type}})
in
    #"Changed Type1"


Then measure as so:

NickolajJessen_0-1655448191043.png

Same syntax for the line measure just with "lin" in stead

 

Thanks, it's a good way, but it seems new data source have to be created and has no relation to CONTEXT, so I am  seeking a way that use DAX 

What?


@null wrote:

Thanks, it's a good way, but it seems new data source have to be created and has no relation to CONTEXT 


I don't think you're gonna reach your goal with DAX only. Some unpivoting will be needed.

Helpful resources

Announcements
August 2022 update 768x460.jpg

Check it Out!

Click here to learn more about the August 2022 updates!

August 1 episode 9_no_dates 768x460.jpg

The Power BI Community Show

Watch the playback when Priya Sathy and Charles Webb discuss Datamarts! Kelly also shares Power BI Community updates.

Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Top Solution Authors
Top Kudoed Authors