简体   繁体   中英

Count active members between 2 dates PowerPivot DAX

Apologies if I make any errors, first time posting here!

I have a dataset that I've read into the Excel data model using PowerQuery, I've split this into 3 tables that I've linked through a unique ID field (so one main table with just the unique IDs and general info then two tables linked from it).

What I want to do is take one of the linked tables that looks like this:

ID Start Date End Date Category
123456 01/01/2000 01/01/2001 A

I've created a separate date table and what I want is a count of every active ID for each month of the date table which I managed using CALCULATE and FILTER in a column on the date table. But when I load that into the Pivot it ignores the categories.

I tried relating the date table using the start date field of the other table but it didn't make any difference.

I've found tonnes of PowerBI solutions that involve calculated tables but being Excel based is a requirement.

Thanks in advance!

I'm afraid that to expand the date interval in Power Query we need to write a line of M code.

This is a small sample that creates a sample table with the columns of the table in your question. I used different value to keep the example simple.

The idea is to expand the dates interval creating a M List, containing the interval of dates expanded. Then to use this list to create the new rows with the new column "Date".

The last step removes the "Start Date" and "End Date" columns

This code can be directly pasted into the advanced query editor for a new blank query.

let
    Source = #table(
        type table
        [
            #"ID"=number, 
            #"Start Date"=date,
            #"End Date"=date,
            #"Category"=text
        ], 
        {
            {1,#date(2020,1,1),#date(2020,1,2), "A"},
            {2,#date(2020,1,10),#date(2020,1,12), "A"}
        }
    ),
    SourceWithList = Table.AddColumn(Source, "Date", 
        each List.Dates([Start Date], Duration.Days([End Date] - [Start Date]) + 1, #duration(1, 0, 0, 0))),
    #"Expanded DateList" = Table.ExpandListColumn(SourceWithList, "Date"),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded DateList",{"Start Date", "End Date"})
in
    #"Removed Columns"

The Source statement is just needed for the example, to create the starting table.

The SourceWithList is the M code to be written: it adds a column using the function Table.AddColumn(), and creates the new column using the function List.Dates().

This function requires the start date, the duration and the step interval.

The duration is computed with the function Duration.Days() that returns the difference between two dates as number of days.

To create the #"Expanded DateList" step it's possible to use the Power Query interface clicking on "Expand to new rows" in the column menu. The screenshot I took are in Power BI, but the Power Pivot interface for Power Query is very similar.

扩展到新行

Then remove the "Start Date" and "End Date" columns by selecting the column and clicking on "Remove Columns"

删除列

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM