Power Query - Counting Distinct entries by group

davey4444

Board Regular
Joined
Nov 16, 2010
Messages
97
Hello, as per the thread title, I am trying to produce a report which shows how many journeys per day our vans are doing from data built up from one row per individual journey.
I've been able to get a pivot table to perform this calculation by using Distinct Count of van registrations plus then the number of journeys. I'm now trying to get Power Query to perform this calculation but am struggling with the distinct count element.
My data is arranged like the table below -
CountryRegistrationVan TypeDateValid Journey?
EnglandABC123Small1/5/20Y
EnglandABC123Small1/5/20Y
ScotlandDEF123Small1/5/20Y
EnglandABC123Small2/5/20Y
EnglandZYX123Large2/5/20Y
EnglandDFG991Large2/5/20Y
EnglandDFG991Large2/5/20Y
ScotlandDEF123Small2/5/20Y
ScotlandDEF123Small1/5/20N

With my final output I'd like to show -

CountryVan TypeDateNumber of distinct vansNumber of valid journeys
EnglandSmall1/5/2012
ScotlandSmall1/5/2011
EnglandSmall2/5/2011
EnglandLarge2/5/2023
ScotlandSmall2/5/2011

When I've tried the Group By function in Power Query I am only able to show the total number of entries (so no "Valid Journey" filtering) and I'm not at all able to get a distinct calculation of vans. Power Query is very new to me and I can see the potential for what I want to do with it but just cannot get it to work as I want at the moment.

Thanks for any help.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
you can try Pivot Table

pt.png

but how you counted this?
row.png
 
Upvote 0
Are you trying to use Power Query (solution in M) or Power Pivot (solution in DAX)? I will assume DAX, though first you'll want to use PowerQuery to load the data into the data model.

You want to count the number of journeys by registration it seems, assuming that a registration always belongs to the same van and a van is always the same size.

So load the table into the data model and call it Journeys. You'll want two measures in your table. The number of journeys will simply be:

VBA Code:
[Van Journey Count]:=CALCULATE (
    COUNTROWS (Journeys),
    Journeys[Valid Journey?] = "Y"
)

The count of van type is a bit trickier because you want the count of registrations within the van type. So this works, at least on the small sample of data you provided:

VBA Code:
[Van Type Count]:=CALCULATE (
    COUNTROWS (
        SUMMARIZE (
            Journeys,
            Journeys[Van Type], Journeys[Registration]
        )
    ),
    Journeys[Valid Journey?] = "Y"
)

That summarizes by van type and registration

Book2
JKLMN
28DateCountryVan TypeVan Type CountVan Journey Count
291/5/2020EnglandSmall12
301/5/2020ScotlandSmall11
312/5/2020EnglandLarge23
322/5/2020EnglandSmall11
332/5/2020ScotlandSmall11
Sheet1
 
Upvote 0
Expected result with Power Query
wpq.png


Rich (BB code):
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Type = Table.TransformColumnTypes(Source,{{"Date", type date}}),
    Custom = Table.AddColumn(Type, "Custom", each 1),
    Group = Table.Group(Custom, {"Country", "Van Type", "Date", "Valid Journey?"}, {{"Journal", each List.Sum([Custom]), type number}, {"Van", each Table.RowCount(Table.Distinct(_)), type number}}),
    Filter = Table.SelectRows(Group, each ([#"Valid Journey?"] = "Y")),
    TSC = Table.SelectColumns(Filter,{"Country", "Van Type", "Date", "Van", "Journal"})
in
    TSC
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top