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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Hijbul_Bari
Frequent Visitor

Dax Number of Occurrence between Date and with Previous Date

Guys, Please help me with DAX code to identify count of single occurrence of "Code" between date and previous date (Two Days).

 

Date

Code

2/1/2022

Subscription_ACCBalance_QFTR

2/1/2022

Package_AKASH Lite Plus_RFTR

2/1/2022

Package_AKASH LITE_RFTR

2/1/2022

Subscription_Service Act_QFTR

2/1/2022

Package_AKASH Lite Plus_RFTR

2/1/2022

Camp Offer_Referral Offer_RFTR

2/2/2022

Content_Specefic Channel_CFTR

2/2/2022

Subscription_ACCBalance_QFTR

2/2/2022

Package_AKASH STANDARD_RFTR

2/2/2022

Camp Offer_Referral Offer_QFTR

2/2/2022

Error Code_E17-0_CFTR

2/3/2022

Package_AKASH STANDARD_RFTR

2/3/2022

Package_AKASH STANDARD_QFTR

2/3/2022

Package_Package Info_QFTR

2/3/2022

Package_AKASH LITE_QFTR

2/4/2022

Subscription_ACCBalance_QFTR

2/4/2022

Content_Specefic Channel_QFTR

2/4/2022

Info Update_HelpPage_CFTR

2/4/2022

Purchase_General Process_QFTR

2/4/2022

Package_AKASH STANDARD_RFTR

2/4/2022

Recharge_bKash_QFTR

2/4/2022

Camp Offer_Feb Recharge_QFTR

2/4/2022

Subscription_Classific_QFTR

 

Output Will be

DateCount=1Total% of 1
2/1/20225683%
2/2/202271164%
2/3/20227978%
2/4/2022101283%
Total293876%

 

Note: When Date will be 2/2/2022 then it will check both 2/1/2022 and 2/2/2022, for  2/3/2022 then it will check both 2/2/2022 and 2/3/2022. Every time it will check number of occurrence =1 from both dates  

1 ACCEPTED SOLUTION
v-cgao-msft
Community Support
Community Support

Hi @Hijbul_Bari ,

 

Please try these measures.

 

 

Count1 = 
VAR _totalcount = 
CALCULATE (
    COUNT ( 'Table'[Code] ),
    DATESINPERIOD ( 'Table'[Date], MAX ( 'Table'[Date] ), -2, DAY )
)
VAR _distinctcount =
CALCULATE (
    DISTINCTCOUNT ( 'Table'[Code] ),
    DATESINPERIOD ( 'Table'[Date], MAX ( 'Table'[Date] ), -2, DAY )
)
RETURN
2*_distinctcount-_totalcount
Total = 
CALCULATE (
    COUNT ( 'Table'[Code] ),
    DATESINPERIOD ( 'Table'[Date], MAX ( 'Table'[Date] ), -2, DAY )
)
% of 1 = [Count1]/[Total]

 

Put them and the date column into the table visual. The result should be like this.

vcgaomsft_0-1645172206848.png

Attach the pbix file for reference. Hope it helps.

 

Best Regards,
Community Support Team_Gao

If there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

 

View solution in original post

5 REPLIES 5
v-cgao-msft
Community Support
Community Support

Hi @Hijbul_Bari ,

 

Please try these measures.

 

 

Count1 = 
VAR _totalcount = 
CALCULATE (
    COUNT ( 'Table'[Code] ),
    DATESINPERIOD ( 'Table'[Date], MAX ( 'Table'[Date] ), -2, DAY )
)
VAR _distinctcount =
CALCULATE (
    DISTINCTCOUNT ( 'Table'[Code] ),
    DATESINPERIOD ( 'Table'[Date], MAX ( 'Table'[Date] ), -2, DAY )
)
RETURN
2*_distinctcount-_totalcount
Total = 
CALCULATE (
    COUNT ( 'Table'[Code] ),
    DATESINPERIOD ( 'Table'[Date], MAX ( 'Table'[Date] ), -2, DAY )
)
% of 1 = [Count1]/[Total]

 

Put them and the date column into the table visual. The result should be like this.

vcgaomsft_0-1645172206848.png

Attach the pbix file for reference. Hope it helps.

 

Best Regards,
Community Support Team_Gao

If there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

 

Hijbul_Bari
Frequent Visitor

amitchandak
Super User
Super User

@Hijbul_Bari , with help from date table, Create a measure like

 

Rolling 2 = CALCULATE(Count(Table[Code]),DATESINPERIOD('Date'[Date ],MAX('Date'[Date ]),-2,DAY))

Rolling 2 = CALCULATE(Count(Table[Code]),DATESINPERIOD('Date'[Date ],MAX('Date'[Date ]),-2,DAY)), 

it give me the total column but I also need the Count column where count of Code=1.

 

DateCount of Code=1
2/1/20225
2/2/20227
2/3/20227
2/4/202210

CALCULATE(DISTINCTCOUNT(data[Code]),DATESINPERIOD(data[Date],MAX(data[Date]),-2,DAY))

 

Can you jusst help me to get only those count which is not duplicate, DistinctCount returns both single and duplicate count. I want only single count from the above formula. Any ideas..

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors