Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
dexter2424
Helper I
Helper I

Distinct Count Total issue based on Type and latest period

Hi All, 

 

I'm struggling with an issue, maybe it's easy to solve, or not, but I'm get lost, and the distinctcount doesn't work properly for me.

I have the following table

Policy NumberReporting PeriodType
A12021-01-01Large
A12021-02-01Not Large
A12021-03-01Large
A12021-05-01Large
B22021-01-01Not Large
B22021-03-01Not Large
B22021-05-01Large


I would like to distinct count the number of "Large" statuses with the following criteria:
I have a slicer, what shows the Max Reporting period date.
Once I select for example 2021-05-01 (YYYY-MM-DD), I would like to see the distinctcount result as 2, because on 2021-05-01 I have 2 Large values.
Once I select 2021-04-01, I would like to see 1, because A1 was Large on 2021-03-01 and B2 was Not Large on 2021-03-01. 
Once I select 2021-02-01, I would like to see 0, because A1 on the selected period is "Not Large", and B2 is also a "Not Large"

In summary, if there is an exact match with the dates, then count as 1 if the Type is "Large", and doesn't count if the Type is "Not Large", 
If there is no record in the selected period (for example A1 on 2021-04-01) then check the second highest date, what is 2021-03-01 and if it's "Large" count as 1, if it's "Not Large" count as 0.

Thank you very much in Advance!

 

Large = 
var _max = CALCULATE(MAXX(Table,Table[Reporting Period]),ALLEXCEPT(Table,Table[Policy Number],Table[Type]))

return

CALCULATE(DISTINCTCOUNT(Table[Policy Number]),FILTER(ALLEXCEPT(Table,Table[Policy Number]),Table[Type] = "Large" && Table[Reporting Period] = _max))

 

1 ACCEPTED SOLUTION
DataInsights
Super User
Super User

@dexter2424,

 

Try this solution.

 

1. Create date table with a relationship to the data table:

 

DataInsights_4-1623158152797.png

 

2. Create measure:

 

Large = 
VAR vSlicerEndDate =
    LASTDATE ( ALLSELECTED ( Dates[Date] ) )
VAR vResult =
    SUMX (
        VALUES ( FactTable[Policy Number] ),
        VAR vMaxDate =
            CALCULATE (
                MAX ( FactTable[Reporting Period] ),
                FactTable[Reporting Period] <= vSlicerEndDate
            )
        VAR vType =
            CALCULATE ( MAX ( FactTable[Type] ), FactTable[Reporting Period] = vMaxDate )
        RETURN
            IF ( vType = "Large", 1, 0 )
    )
RETURN
    vResult

 

3. Create date slicer using the date table (Dates[Date]).

 

4. Create visual:

 

DataInsights_1-1623157987040.png

 

---------------------------------------------

 

DataInsights_2-1623158000229.png

 

---------------------------------------------

 

DataInsights_3-1623158045520.png

 

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

5 REPLIES 5
DataInsights
Super User
Super User

@dexter2424,

 

Try this solution.

 

1. Create date table with a relationship to the data table:

 

DataInsights_4-1623158152797.png

 

2. Create measure:

 

Large = 
VAR vSlicerEndDate =
    LASTDATE ( ALLSELECTED ( Dates[Date] ) )
VAR vResult =
    SUMX (
        VALUES ( FactTable[Policy Number] ),
        VAR vMaxDate =
            CALCULATE (
                MAX ( FactTable[Reporting Period] ),
                FactTable[Reporting Period] <= vSlicerEndDate
            )
        VAR vType =
            CALCULATE ( MAX ( FactTable[Type] ), FactTable[Reporting Period] = vMaxDate )
        RETURN
            IF ( vType = "Large", 1, 0 )
    )
RETURN
    vResult

 

3. Create date slicer using the date table (Dates[Date]).

 

4. Create visual:

 

DataInsights_1-1623157987040.png

 

---------------------------------------------

 

DataInsights_2-1623158000229.png

 

---------------------------------------------

 

DataInsights_3-1623158045520.png

 

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Hi,

 

I am using this solution and it works great thanks.  I have a quick question.  If I wanted to display this measure in a bar chart instead with the value for each month acros x axis how could that be achieved?  Basically I would like to not have the slider but do the calculation for using the value of the first date of each month instead.  Thanks

Thanks you DataInsights! That's what I'm looking for.

In the meantime I have figured out another solution what is working, I just leave it here, maybe somebody findes it useful:

Creating a max date measure:

Max_Date = CALCULATE(MAXX(Table,Table[Reporting Period]),ALLEXCEPT(Table,Table[Policy Number]))

and the main measure:

Large = COUNTX(FILTER(Table,Table[Type] = "Large" && Table[Reporting Period] = [Max_Date]),Table[Policy Number])



amitchandak
Super User
Super User

@dexter2424 , Try a measure like

 

measure =
Var _max = maxx(allselected(Table), Table[Date])
var _max1 = maxx(filter(Table, Table[date] <_max), Table[Date])
var _cnt = countx(filter(Table, Table[Date] =_max && Table[Type] ="Large"),[Policy Number])+0
var _cnt1 = countx(filter(all(Table), Table[Date] =_max1 && Table[Type] ="Large"),[Policy Number])
return
if(_cnt =0, _cnt1, _cnt)

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Thanks Amitchandak, but it still picks up the latest date.

I have 4 different policies, and the highest date is 01-04-2021 there, all of the policies are unique, and for 3 of them the latest date is 01-04-2021 and one of them is 01-03-2021. The result of your query above gives me 3, because just counts the 3 highest ones, where the max date is 01-04-2021 and ignore the 01-03-2021 in another policy

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.