-2

I do Power BI for a logistics company. We want to show performance by stop location. The data is currently a table of all orders by Order ID, so -- ID, Rev $, Pickup Stop, Delivery Stop. Everything is a 2-stop load, fortunately.

What I am struggling with is building a calculated table that looks at the Pickup Stop AND the Delivery Stop at the same time while ALSO respecting filters set on the page. I would like the stops table to say something like: Stop Location, X Pickups, $X Pickup Revenue, X Deliveries, $X Delivery Revenue.

How would I go about this? I've tried a number of approaches but every time it either misses filters or can only handle one stop at a time.

Thanks!

Current Data
call it Orders
Current Data, call it Orders

The calculated table I'm trying to make
call it Stops
The calculated table I'm trying to make, call it Stops

Ron Rosenfeld
  • 53,870
  • 7
  • 28
  • 60
  • Your two tables are identical. Making it quite difficult to know what you want. – Ron Rosenfeld Aug 26 '21 at 01:23
  • Apologies. I struggled with the formatting of images in the question publisher. Should be corrected now. – Evan Swanzy Aug 26 '21 at 17:22
  • How would your output look if there were multiple delivery/pickups per Stop? Multiple `X`'s? Something Else? – Ron Rosenfeld Aug 26 '21 at 17:33
  • They'd be summary values. I had some success by creating two calculated tables like so, then joining them at Pickup/Delivery Stops. However I don't know how to join to the Orders table and still respect slicers/filters. Pickup Stops = summarize(Orders, Orders[Pickup Stop], "Outbound Orders", DISTINCTCOUNT(Orders[Order ID]), "Outbound Revenue", sum(Orders[Total Revenue])) Delivery Stops = summarize(Orders, Orders[Delivery Stop], "Inbound Orders", DISTINCTCOUNT(Orders[Order ID]), "Inbound Revenue", sum(Orders[Total Revenue])) – Evan Swanzy Aug 26 '21 at 18:09

1 Answers1

0

One method of creating your Stops, given your Orders is by using Power Query, accessed via Queries=>Transform Data on the Power BI Home Tab.

The Table.Group function is where the magic happens. Unfortunately, it needs to be done by coding in the Advanced Editor, as the UI does not provide for these custom aggregations.

  • When the PQ Editor opens: Home => Advanced Editor
  • The first three lines should be replaced by whatever you are reading in your own Orders table with.
  • Paste the rest of M Code below in place of what is below your setup lines in your own query
  • Read the comments and explore the Applied Steps to understand the algorithm

M Code

let
    //Input data and set datatypes
    //These lines should be replaced with whatever you need to 
    //set up your data table
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bYzBCoMwEER/Zck5BxPRu1RaLJaW6qEQPIS4tEExoonQv+/a0oLQyw5vZnaUYuepxQmKnHFWO697uOKCQ0DiizVdGKHybiTKsbcLTs8PN1wxIZMooiR938z3evCawyFbKczeDhzq268qyBZpsg23f9+qJF+Skuwe1ui741CU/2djsmO53lJ3SFsth/3aPWrTzY7Kp4o1zQs=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t]),
    #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    dataSource = Table.TransformColumnTypes(#"Promoted Headers",{
        {"Order ID", Int64.Type}, {"Total Revenue", Int64.Type}, 
        {"Pickup Stop", type text}, {"Delivery Stop", type text}}),

    //Unpivot to get single column of Stops
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(dataSource, {"Order ID", "Total Revenue"}, "Attribute", "Stop"),

    //Group by stop and do the aggregations
    #"Grouped Rows" = Table.Group(#"Unpivoted Columns", {"Stop"}, {
        {"Orders Picked Up", (t)=> List.Count(List.Select(t[Attribute], each _ = "Pickup Stop" )), Int64.Type},
        {"Total Revenue Picked Up", (t)=> List.Sum(Table.SelectRows(t, each [Attribute]="Pickup Stop")[Total Revenue]), type number},
        {"Orders Delivered", (t)=> List.Count(List.Select(t[Attribute], each _ = "Delivery Stop" )), Int64.Type},
        {"Total Revenue Delivered", (t)=> List.Sum(Table.SelectRows(t, each [Attribute]="Delivery Stop")[Total Revenue]), type number}
        })
in
    #"Grouped Rows"

Orders
enter image description here

Stops
enter image description here

Ron Rosenfeld
  • 53,870
  • 7
  • 28
  • 60
  • Thanks. I'm following your instructions but I don't have any experience with Power Query here to plug in the original source correctly. Sounds like I need to study PQ from scratch if I intend to go that route. – Evan Swanzy Aug 26 '21 at 19:37
  • @EvanSwanzy If you have your table in Power BI, in your **Orders** format, select the Transform option should open PQ. Then select Home=>Advanced Editor and you should be able to see the PQ code. Then you should be able to add the `unpivot` and `group` code at that point. But familiarize yourself with how it works to get the syntax to merge properly with the previous code. – Ron Rosenfeld Aug 26 '21 at 19:47