VERBOSE
VERBOSE

Reputation: 1085

How to make sure a PowerQuery pivot holds some pre defined values?

My input is a table :

enter image description here

And I need to do a custom pivot where I make sure that :

My expected output is a table like below :

+------------+---------------+-------------+-----------+------------+
|   TO START |   IN PROGRESS |   CANCELLED |   STANDBY |   FINISHED |
|------------+---------------+-------------+-----------+------------|
|          6 |            13 |           1 |         0 |         14 |
+------------+---------------+-------------+-----------+------------+

But my code gives me this right now :

enter image description here

Can you guys show me how to do that ?

Here is the minimal reproducible code used to generate my example :

let
  Source = Table.FromRows(
    Json.Document(
      Binary.Decompress(
        Binary.FromText(
          "i45W8vRTCAjydw9yDQ5W0lEyUorVQRczBou5efp5Bnu4ugAFTMECzo5+zq4+PmARA7BIiL9CcIhjUAhcD7ISQ+xKkIzFZrcFuiJzpdhYAA==",
          BinaryEncoding.Base64
        ),
        Compression.Deflate
      )
    ),
    let
      _t = ((type nullable text) meta [Serialized.Text = true])
    in
      type table [ID = _t, HOW_MANY = _t]
  ),
  Types = Table.TransformColumnTypes(Source, {{"ID", type text}, {"HOW_MANY", Int64.Type}}),
  Pivot = Table.Pivot(Types, List.Distinct(Types[ID]), "ID", "HOW_MANY", List.Sum)
in
  Pivot

Upvotes: 1

Views: 30

Answers (1)

davidebacci
davidebacci

Reputation: 30174

UPDATE

enter image description here

let
  Source = Table.FromRows(
    Json.Document(
      Binary.Decompress(
        Binary.FromText(
          "i45W8vRTCAjydw9yDQ5W0lEyUorVQRczBou5efp5Bnu4ugAFTMECzo5+zq4+PmARA7BIiL9CcIhjUAhcD7ISQ+xKkIzFZrcFuiJzpdhYAA==",
          BinaryEncoding.Base64
        ),
        Compression.Deflate
      )
    ),
    let
      _t = ((type nullable text) meta [Serialized.Text = true])
    in
      type table [ID = _t, HOW_MANY = _t]
  ),
  Types = Table.TransformColumnTypes(Source, {{"ID", type text}, {"HOW_MANY", Int64.Type}}),
    Dummy = #table({"ID", "HOW_MANY"},{ {"TO START",0},{"IN PROGRESS",0} ,{"CANCELLED",0},{"STANDBY",0},{"FINISHED",0}   }) & Types,
  Pivot = Table.Pivot(Dummy, List.Distinct(Dummy[ID]), "ID", "HOW_MANY", List.Sum)
in
  Pivot

Upvotes: 2

Related Questions