cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
mfa
Frequent Visitor

Arithmetic from diff periods of time off of the same table

 

Dear Community,

 

during any given time I'm trying to create a measure that will help me count the actives of N Days Ago

for example, during 3rd of Jan if I want to count of actives 2 days ago it would be 8.

 

and 4-Jan (current)(8) minus 1-Jan (2 days)(6) ago would be =  8 (Active Occurances) - 6 (Active Occurances) = 5

 

DateActive
1-Jan 
1-Jan 
1-Jan 
1-Jan 
1-Jan 
1-JanActive
1-JanActive
1-JanActive
1-JanActive
1-JanActive
1-JanActive
1-JanActive
1-JanActive
2-JanActive
2-JanActive
2-JanActive
2-JanActive
2-JanActive
2-JanActive
2-JanActive
2-JanActive
2-JanActive
3-JanActive
3-JanActive
3-JanActive
3-Jan 
3-Jan 
3-Jan 
3-Jan 
3-Jan 
3-Jan 
3-Jan 
3-Jan 
3-Jan 
3-Jan 
4-Jan 
4-Jan 
4-JanActive
4-JanActive
4-JanActive
4-JanActive
4-JanActive
4-JanActive
5-JanActive
5-JanActive
5-JanActive
5-JanActive
5-JanActive
5-JanActive
5-JanActive
5-Jan 
5-Jan 
5-Jan 
5-Jan 
5-Jan 
5-Jan 
5-Jan 
5-Jan 
5-Jan 

 

I'm using days as an example but what I really want is to go back months behind.

So Far I've tried a simple Date Count.

 

 

 

Count_Measure = CALCULATE(COUNT(Table[Date]),LASTDATE(date(year(Table[Date]),MONTH(Table[Date]),DAY(Table[Date])-2)))

 

 

 it didn't work

thanks in advance

 

1 ACCEPTED SOLUTION
RicoZhou
Community Support
Community Support

Hi @mfa 

I think you can build two slicer to select the date you need in your measure.

Build two calculated tables to create slicer.

Current Date = VALUES('Table'[Date])
Previous Date = VALUES('Table'[Date])

Measure:

Difference =
VAR _Previous =
    SELECTEDVALUE ( 'Previous Date'[Date] )
VAR _Current =
    SELECTEDVALUE ( 'Current Date'[Date] )
VAR _CountPreviousActive =
    CALCULATE (
        COUNTROWS ( 'Table' ),
        'Table'[Date] = _Previous,
        'Table'[Active] = "Active"
    )
VAR _CountCurrentActive =
    CALCULATE (
        COUNTROWS ( 'Table' ),
        'Table'[Date] = _Current,
        'Table'[Active] = "Active"
    )
RETURN
    ABS ( _CountCurrentActive - _CountPreviousActive )

Result is as below.

1.png

 

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

5 REPLIES 5
Greg_Deckler
Super User
Super User

@mfa Seems like if you have a date range slicer you could do something like this:

Measure =
  VAR __MaxDate = MAX('Date'[Date])
  VAR __MinDate = MIN('Date'[Date])
RETURN
  COUNTROWS(FILTER('Table',[Date]<=__MaxDate && [Date] >=__MinDate && [Active]="Active"))

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Mastering Power BI 2nd Edition

DAX is easy, CALCULATE makes DAX hard...

thank you for your quick reply,

 

im not after a range, im trying to compare between two individual dates

@mfa Whether a date range slicer or not, the same logic applies. Get a date somehow. Use simple math like [Date] - 30 or a whole host other other ways to caclulate your other date. Might help if I understood what you actually wanted as output because the example you provided doesn't make any sense 8 - 3 = 5, I have no idea where you are getting those numbers from the sample data provided.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Mastering Power BI 2nd Edition

DAX is easy, CALCULATE makes DAX hard...

Correction: it would be 8-6

 

number of actives in 4th of jan minus number of actives in 1st of jan

 

I'm imagining something like this 

Calculate(Count(Date[Date]), Date[Date] = Date[Date] - 3)

But obviously, this didn't work

RicoZhou
Community Support
Community Support

Hi @mfa 

I think you can build two slicer to select the date you need in your measure.

Build two calculated tables to create slicer.

Current Date = VALUES('Table'[Date])
Previous Date = VALUES('Table'[Date])

Measure:

Difference =
VAR _Previous =
    SELECTEDVALUE ( 'Previous Date'[Date] )
VAR _Current =
    SELECTEDVALUE ( 'Current Date'[Date] )
VAR _CountPreviousActive =
    CALCULATE (
        COUNTROWS ( 'Table' ),
        'Table'[Date] = _Previous,
        'Table'[Active] = "Active"
    )
VAR _CountCurrentActive =
    CALCULATE (
        COUNTROWS ( 'Table' ),
        'Table'[Date] = _Current,
        'Table'[Active] = "Active"
    )
RETURN
    ABS ( _CountCurrentActive - _CountPreviousActive )

Result is as below.

1.png

 

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Carousel_PBI_Wave1

2023 Release Wave 1 Plans

Power BI release plans for 2023 release wave 1 describes all new features releasing from April 2023 through September 2023.

Power BI Summit Carousel 2

Global Power BI Training

Make sure you register today for the Power BI Summit 2023. Don't miss all of the great sessions and speakers!

Thank you 2022 Review

2022 Monthly Feature Releases

We had a great 2022 with a ton of feature releases to help you drive a data culture.