Forum Discussion

Gerbil's avatar
Gerbil
Helper I
1 year ago
Solved

Matrix Values from Different Columns in Data Source?

Let's say I have a data source that looks like this:

IDABCDEFGHI
1abcdefghi
2abcdefghi
3abcdefghi
4abcdefghi
5abcdefghi

 

I'd like to make a table like so:

 Col1Col2Col3
Row1abc
Row2def
Row3ghi
 sum(col1)sum(col2)sum(col3)

 

Where I can put in a custom column header and row header, as well as get totals of each column. This table will be filtered with a slicer to a specific ID, so it will display all the values associated with that ID.

 

Is this possible?

  • yes, that can be done as well.

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUUoE4iQgTgbiFCBOBeI0IE4H4gwgzlSK1YlWMiJapTHRKk2IVmlKnMpYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, A = _t, B = _t, C = _t, D = _t, E = _t, F = _t, G = _t, H = _t, I = _t]),
        #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"ID"}, "Category", "Value"),
        #"Added Index" = Table.AddIndexColumn(#"Unpivoted Other Columns", "Row", 1, 1, Int64.Type),
        #"Replaced Value" = Table.ReplaceValue(#"Added Index",each [Row],each let n=Int64.From(Number.Mod([Row]+1,9)/3) in if n=0 then 3 else n,Replacer.ReplaceValue,{"Row"})
    in
        #"Replaced Value"

3 Replies

  • That's a standard "Unpivot in groups"  pattern.  What have you tried and where are you stuck?

    • Gerbil's avatar
      Gerbil
      Helper I

      I looked into unpivoting and it looks like a solution I can use. But can I change the query so that the table looks like this:

       

      IDCategoryRowValue
      1A1a
      1B1b
      1C1c
      1D2d
      1E2e
      1F2f
      1G3g
      1H3h
      1I3i

       

       

      i.e. have two clauses for the category

      • lbendlin's avatar
        lbendlin
        Super User

        yes, that can be done as well.

         

        let
            Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUUoE4iQgTgbiFCBOBeI0IE4H4gwgzlSK1YlWMiJapTHRKk2IVmlKnMpYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, A = _t, B = _t, C = _t, D = _t, E = _t, F = _t, G = _t, H = _t, I = _t]),
            #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"ID"}, "Category", "Value"),
            #"Added Index" = Table.AddIndexColumn(#"Unpivoted Other Columns", "Row", 1, 1, Int64.Type),
            #"Replaced Value" = Table.ReplaceValue(#"Added Index",each [Row],each let n=Int64.From(Number.Mod([Row]+1,9)/3) in if n=0 then 3 else n,Replacer.ReplaceValue,{"Row"})
        in
            #"Replaced Value"