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
SamFletch
Helper I
Helper I

Calculating Days Based On Dates & Criteria

Hello all, I've been scratching my head with a problem for a few days now and I just can't seem to figure it out, so I was hoping you could help.

 

I'm trying to calculate how many days there are between 2 dates, with a slight twist. 

Let's give you an example of my data and I'll show you what I mean. So let's say I have a product that has a few dates, an "activated date" a "deactived date"  and a toggle as to whether it is currently active. It looks a little like this:

 

ProductActivate DateDeactivate DateIs Active
A01/01/2010/10/20No
B01/01/2010/10/20No
C18/03/20 Yes
D24/04/20 Yes
A01/01/21 Yes
E01/01/2112/01/21No
F01/01/2115/01/21No
G12/01/21 Yes
H16/01/21 Yes

 

As you can see, a produce can have multiple days active (as in the case of product A that was active from from 01/01/20 until 10/10/20 then again from 01/01/21. Other than that, a product can be active or inactive at any given period.

 

I am trying to create a custom column which shows how many days in the current month that a product has been active which also assumes that any product currently active is going to run until the end of the month. The new column would look like the below:

 

ProductActivate DateDeactive DateIs ActiveDays Active This Month 
A01/01/2010/10/20No0
B01/01/2010/10/20No0
C18/03/20 Yes31
D24/04/20 Yes31
A01/01/21 Yes31
E01/01/2112/01/21No12
F01/01/2115/01/21No15
G12/01/21 Yes19
H16/01/21 Yes15

 

So looking it the new column, it assumes that anything active as of the 01/01/21(C & D in the table) are going to be active the full 31 days, any vehicles activated in January this year (A, G & H in the table) are going to run to the end of it and any that were deactived in January (E & F in the table) will only be active the number of days they were actually active for. 

 

I hope this all makes sense so far.

 

Now I wrote a piece of DAX that I am actually pretty proud of but is not quite working right: 

 

Days Active This Month = 

SWITCH(
True(),

'Product'[Is Active] = "Yes" && MONTH('Product'[Activate Date]) <> MONTH (MAX('Product'[Activate Date])), DATEDIFF (STARTOFMONTH (TODAY()), ENDOFMONTH (TODAY()), DAY),
'Product'[Is Active] = "Yes" && MONTH('Product'[Activate Date]) = MONTH ( MAX('Product'[Activate Date])), DATEDIFF ( 'Product'[Activate Date], ENDOFMONTH (TODAY()), DAY ),
'Product'[Is Active] = "No" && MONTH('Product'[Activate Date]) = MONTH ( MAX( MAX('Product'[Activate Date])), DATEDIFF ('Product'[Activate Date], 'Product'[Deactivate Date] , DAY ),
'Product'[Is Active] = "No" && MONTH('Product'[Activate Date]) <> MONTH ( Max('Product'[Activate Date])), DATEDIFF ( STARTOFMONTH(TODAY()), 'Product'[Deactivate Date], DAY),
0
)

 

This feels like it is on the right track but the results are just not coming back quite right. EDIT: Link to google doc with results

 

Firstly, I know that I'm doing wrong with the way I'm calculating if the product was active at the start of the month or not. MONTH just doesn't cut it but I'm struggling to write the formula to figure out  if the product was active at the start of the month or not.

 

Other than that though, I feel like it should be working but I'm not confident it is.

 

Any help with this is greatly appreciated, I hope I've made myself clear but I can understand that I may not have, so any questions, please feel free to send my way. 

 

Thank you.

1 ACCEPTED SOLUTION
Fowmy
Super User
Super User

@SamFletch 

Add the following code as a new column to your table. Check with other scenarios and reply with your findings.

Days Active This Month = 
var _thismonth = (EOMONTH(TODAY(),0))
var _active = T3[Is Active] = "Yes"
return
IF(
    _active,
   Min(MIN( _thismonth - T3[Activate Date] + 1 , _thismonth ), DAY(_thismonth)),
   Max(0,T3[Deactivate Date] - (_thismonth - DAY(_thismonth)) )   
)

 

Fowmy_1-1611414084332.png

 

 



Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

View solution in original post

5 REPLIES 5
Fowmy
Super User
Super User

@SamFletch 

Add the following code as a new column to your table. Check with other scenarios and reply with your findings.

Days Active This Month = 
var _thismonth = (EOMONTH(TODAY(),0))
var _active = T3[Is Active] = "Yes"
return
IF(
    _active,
   Min(MIN( _thismonth - T3[Activate Date] + 1 , _thismonth ), DAY(_thismonth)),
   Max(0,T3[Deactivate Date] - (_thismonth - DAY(_thismonth)) )   
)

 

Fowmy_1-1611414084332.png

 

 



Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

Wow this worked and you managed to do it in less than 10 lines of code! 

 

Thank you so much, I'm very impresed and incredibly grateful! Would you mind explaining a little how it works? I understand the creation of the variables and honestly should have thought about doing that myself and I didn't realise EOMONTH was a thing, I'm just struggling to understand how the MIN and MAX lines are working. 

@SamFletch 

I noted that in my formula, I did not account for Activation dates within the month for Status NO, try this code instead of the above.

Days Active This Month = 
var _thismonth = (EOMONTH(TODAY(),0))
var _active = T3[Is Active] = "Yes"
return
IF(
    _active,
   MIN(
       _thismonth - T3[Activate Date] + 1 , 
       DAY(_thismonth)
    ),
    IF(
        ISBLANK( T3[Deactivate Date] ), 
            0 , 
            COUNTROWS(
                INTERSECT(
                        CALENDAR(EOMONTH(_thismonth,-1),_thismonth),
                        CALENDAR(T3[Activate Date],T3[Deactivate Date])
                )
            )
    )
)



 

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

vanessafvg
Super User
Super User

when you say the results are not coming back right can you give an example of what is not right.  

 

I would also probably write it like this

 

Days Active This Month =
SWITCH (
    TRUE (),
    AND (
        'Product'[Is Active] = "Yes",
        MONTH ( 'Product'[Activate Date] ) <> MONTH ( MAX ( 'Product'[Activate Date] ) )
    )DATEDIFF ( STARTOFMONTH ( TODAY () )ENDOFMONTH ( TODAY () )DAY ),
    AND (
        'Product'[Is Active] = "Yes",
        MONTH ( 'Product'[Activate Date] ) = MONTH ( MAX ( 'Product'[Activate Date] ) )
    )DATEDIFF ( 'Product'[Activate Date], ENDOFMONTH ( TODAY () )DAY ),
    AND (
        'Product'[Is Active] = "No",
        MONTH ( 'Product'[Activate Date] ) = MONTH ( MAX ( 'Product'[Activate Date] ) )
    )DATEDIFF ( 'Product'[Activate Date], 'Product'[Deactivate Date], DAY ),
    AND (
        'Product'[Is Active] = "No",
        MONTH ( 'Product'[Activate Date] ) <> MONTH ( MAX ( 'Product'[Activate Date] ) )
    )DATEDIFF ( STARTOFMONTH ( TODAY () ), 'Product'[Deactivate Date], DAY ),
    0
)





If I took the time to answer your question and I came up with a solution, please mark my post as a solution and /or give kudos freely for the effort 🙂 Thank you!

Proud to be a Super User!




Sorry I didn't include this at the time. I've downloaded a full report of what the numbers look like and as you can see, they're just all over the place!

 

I've pasted the results into a Google Sheet which can you access with the link below:

 

Link to Doc 

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.