Forum Discussion

DIEGO_RESSU's avatar
DIEGO_RESSU
New Member
1 year ago
Solved

PIVOT TABLE AT POWER QUERY

Hello,

I have a table and I need to do a Pivot Table in Power Query:

 

CityRegionCode
São PauloA10
São PauloB20
São PauloB20
RioA15
RioA25
RioB25
RioB30
RioC30
MadridC11
BarcelonaA14
PortoA5
PortoB5
PortoC5
PortoC5

 

I need distinct count the colunm of Code and returns something like this:

 

Anybody can help me?

 

Tks.

  • Consider your table, then select column City and right click on it, then pick group by and make the setting provided in the folowing image.

     

     

    it results in the following M code

    = Table.Group(Source, {"City"}, {{"Count", each List.Sum([Region]), type nullable text}})

    rewrite this formula as following

     

    = Table.Group(Source, {"City"}, {{"Count", each List.Count(List.Distinct([Region])), type nullable text}})

    which results in the following

     

     

     

     

    so the whole code for your problem is :

     

    let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCj68OF8hILE0J19JR8kRiA0NlGJ10MSdgNgIv3hQJtwAUzQBI2QBJ2wCxshmOCMEfBNTijJToGKGhmAxp8Si5NSc/LxEmHUmYOGA/KISmIWmKCJOGCLOuERiAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [City = _t, Region = _t, Code = _t]),
    #"Grouped Rows" = Table.Group(Source, {"City"}, {{"Count", each List.Count(List.Distinct([Region])), type nullable text}})
    in
    #"Grouped Rows"

2 Replies

  • Not sure why you would want to do that in Power Query?

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCj68OF8hILE0J19JR8kRiA0NlGJ10MSdgNgIv3hQJtwAUzQBI2QBJ2wCxshmOCMEfBNTijJToGKGhmAxp8Si5NSc/LxEmHUmYOGA/KISmIWmKCJOGCLOuERiAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [City = _t, Region = _t, Code = _t]),
        #"Removed Columns" = Table.RemoveColumns(Source,{"Region"}),
        #"Grouped Rows" = Table.Group(#"Removed Columns", {"City"}, {{"Count", each Table.RowCount(Table.Distinct(_)), Int64.Type}})
    in
        #"Grouped Rows"
  • Consider your table, then select column City and right click on it, then pick group by and make the setting provided in the folowing image.

     

     

    it results in the following M code

    = Table.Group(Source, {"City"}, {{"Count", each List.Sum([Region]), type nullable text}})

    rewrite this formula as following

     

    = Table.Group(Source, {"City"}, {{"Count", each List.Count(List.Distinct([Region])), type nullable text}})

    which results in the following

     

     

     

     

    so the whole code for your problem is :

     

    let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCj68OF8hILE0J19JR8kRiA0NlGJ10MSdgNgIv3hQJtwAUzQBI2QBJ2wCxshmOCMEfBNTijJToGKGhmAxp8Si5NSc/LxEmHUmYOGA/KISmIWmKCJOGCLOuERiAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [City = _t, Region = _t, Code = _t]),
    #"Grouped Rows" = Table.Group(Source, {"City"}, {{"Count", each List.Count(List.Distinct([Region])), type nullable text}})
    in
    #"Grouped Rows"