简体   繁体   中英

DAX Calculate a measure based on Max Date of the Month

Let us say I have a table Doc_Table with the following columns:

  • Doc_Ref
  • Version_Date
  • Other_Attributes

A row is unique by combining a Doc_Ref and the Version_Date.

I am searching for calculating the Count of Documents per last version of the month. Example: if I have received 3 versions 11/4, 15/4 and 24/4 on April I would only use the last version for April.

In SQL it would look like:

Select count(Doc_Ref)
From Doc_Table
Where Month(Version_Date) in (Select MAX(Version_Date), Month(Version_Date) From Doc_Table Group By Month(Version_Date))

Let us say we have the following elements:

Doc_Ref ; Version_Date ; Other_Attributes
Ref1 ; 2020-03-20 ; ...
Ref2 ; 2020-03-20 ; ...

Ref1 ; 2020-04-11 ; ...
Ref2 ; 2020-04-11 ; ...
Ref3 ; 2020-04-11 ; ...

Ref1 ; 2020-04-15 ; ...
Ref2 ; 2020-04-15 ; ...
Ref3 ; 2020-04-15 ; ...

Ref1 ; 2020-04-24 ; ...
Ref2 ; 2020-04-24 ; ...
Ref3 ; 2020-04-24 ; ...
Ref4 ; 2020-04-24 ; ...

Expected results would be:

Month Year - Count_Of_Doc_Ref
March 2020 - 2
April 2020 - 4

Could you assist me with the KPI Creation? Thank you for your insights.

EDIT: I come up with that answer based on Agustin Palacios reply

NB of Ongoing Doc Ref Shared = 
VAR VersionLessThan = SELECTEDVALUE('Axis Doc_Ref'[Version Date];MAX('Axis Doc_Ref'[Version Date]))
VAR OngoingEWPerMonth = CALCULATE
    (   [NB of Ongoing Doc Ref]
        ;FILTER('Axis Doc_Ref';'Axis Doc_Ref'[Version Date] = MAX('Axis Doc_Ref'[Version Date]))
    )
RETURN
CALCULATE
(
    CALCULATE
    (   [NB of Ongoing Doc Ref]
        ;FILTER('Axis Doc_Ref';'Axis Doc_Ref'[Version Date] = MAX('Axis Doc_Ref'[Version Date]))
    )
    ;USERELATIONSHIP('Axis Doc_Ref'[Version Date];'Axis EW Created Date'[Date - Created])
    ;FILTER(all('Axis Doc_Ref'[Version Date]); 'Axis Doc_Ref'[Version Date] <= VersionLessThan)
)

First, you have to have a column for the Month and another for the Year. I created a both using:

Column Year

Year = YEAR( 'Table'[Version_Date] )

Column Month

Month = MONTH( 'Table'[Version_Date] )

Then use this measure:

MaxCount =
CALCULATE (
    COUNT ( 'Table'[Doc_Ref] ),
    FILTER ( 'Table', 'Table'[Version_Date] = MAX ( 'Table'[Version_Date] ) )
)

Use Year, Month and MaxCount to create a table. This is the result:

在此处输入图像描述

Hope it helps you.

This also works:

I assume you have a date dimension table (if not, use one). The dimDate table should have a 1:* relationship to 'Doc_Table'[Version_Date]. Then in a visual table add year and month to the table (from the dimDate table) and finaly add this measure to the table:

Distinct Doc_ref = 
DISTINCTCOUNT('Doc_Table'[Doc_ref])

The visual will create the context of year and month and the DISTINCTCOUNT-function will then count how many distinct Doc_ref there are in eg 2020 April (4).

在此处输入图像描述

Try something as follow:

EVALUATE
SUMMARIZE (   
FILTER (
    CALCULATETABLE (
        ADDCOLUMNS (
            RefTable,
            "LastDate",
            VAR MaxDate =
                MAXX (
                    FILTER (
                        RefTable,
                        MONTH ( RefTable[Version_Date] ) = MONTH ( EARLIER ( RefTable[Version_Date] ) )
                    ),
                    RefTable[Version_Date]
                )
            RETURN
                IF ( MaxDate = RefTable[Version_Date], MaxDate ),
            "Month_Year", FORMAT ( RefTable[Version_Date], "MMM YY" )
        )
    ),
    [LastDate] <> BLANK ()
),
[Month_Year],
"Count", COUNTA ( RefTable[Doc_Ref] ) )

Let us know if that works for you.

Best

David

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