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
aashton
Helper V
Helper V

DATEDIFF with SUM not working

In Power BI I've created a table with Pay Period Start Date, Pay Period End DAte, Check Date.  I have a calculated column to find the number of days in a pay period  ( I tried a million variations of DATEDIFF and couldn't get it to work, so tried this):  

Days Pay Period = [PayPeriodEnd] - [PayPeriodStart] + 1
 
I need to find the total number of days where the Check Date was in the last month.  I have a Check Date Flag:  
Check Date Last Month = IF(DATEDIFF('Pay Periods Bi-weekly'[Check Date], TODAY(), MONTH)=1, 1, 0).   This works.
I've tried measures and calculated columns like:  
TotalDaysLastMonth =
SUMX(
    FILTER(
        'Pay Periods Bi-weekly',
        'Pay Periods Bi-weekly'[Check Date Last Month] = 1
    ),
    SELECTEDVALUE('Pay Periods Bi-weekly'[Days Pay Period])
And it just keeps giving me very wrong anwers, off by hundreds.  I can't figure it out. ChatGPT can't figure it out.  Any ideas???
10 REPLIES 10
parry2k
Super User
Super User

@aashton  It will be easier if you share pbix file using one drive/google drive with the expected output. Remove any sensitive information before sharing.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

aashton
Helper V
Helper V

I've tested all of these methods on a different table in my Power BI file, a spreadsheet that I am pulling in.  It all worked perfectly.  This table Pay Periods Bi-Weekly is a calculated table I created to create two week pay periods.  So it must have something to do with it being a calculated table??

Pay Periods Bi-weekly =
VAR StartDate = DATE(2024, 1, 1)
VAR EndDate = DATE(2025, 12, 31)
VAR PayPeriodStart = DATE(2024, 5, 5)
VAR PayPeriodLength = 14
VAR NumberOfDays = DATEDIFF(StartDate, EndDate, DAY)
RETURN
ADDCOLUMNS(
    CALENDAR(StartDate, EndDate),
    "PayPeriodStart",
        VAR CurrentDate = [Date]
        RETURN PayPeriodStart + PayPeriodLength * INT((CurrentDate - PayPeriodStart) / PayPeriodLength),
    "PayPeriodEnd",
        VAR CurrentDate = [Date]
        RETURN PayPeriodStart + PayPeriodLength * INT((CurrentDate - PayPeriodStart) / PayPeriodLength) + PayPeriodLength - 1
)

 

parry2k
Super User
Super User

@aashton send the pbix file and remove sensitive information before sharing.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

aashton
Helper V
Helper V

The code to create the table is, don't know if that affects anything:   

Pay Periods Bi-weekly =
VAR StartDate = DATE(2024, 1, 1)
VAR EndDate = DATE(2025, 12, 31)
VAR PayPeriodStart = DATE(2024, 5, 5)
VAR PayPeriodLength = 14
VAR NumberOfDays = DATEDIFF(StartDate, EndDate, DAY)
RETURN
ADDCOLUMNS(
    CALENDAR(StartDate, EndDate),
    "PayPeriodStart",
        VAR CurrentDate = [Date]
        RETURN PayPeriodStart + PayPeriodLength * INT((CurrentDate - PayPeriodStart) / PayPeriodLength),
    "PayPeriodEnd",
        VAR CurrentDate = [Date]
        RETURN PayPeriodStart + PayPeriodLength * INT((CurrentDate - PayPeriodStart) / PayPeriodLength) + PayPeriodLength - 1
)
parry2k
Super User
Super User

@aashton or try this:

 

TotalDaysLastMonth =
SUMX(
    SUMMARIZE ( 
      FILTER(
          'Pay Periods Bi-weekly',
          'Pay Periods Bi-weekly'[Check Date Last Month] = 1
       ),
       'Pay Periods Bi-weekly'[Pay Period Start Date],
       'Pay Periods Bi-weekly'[Pay Period End Date],
       'Pay Periods Bi-weekly'[Days Pay Period] )
    )
    [Days Pay Period]
)


Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Syntax error on this, missing a comma??

 

TotalDaysLastMonth =
SUMX(
    SUMMARIZE (
      FILTER(
          'Pay Periods Bi-weekly',
          'Pay Periods Bi-weekly'[Check Date Last Month] = 1
       ),
       'Pay Periods Bi-weekly'[PayPeriodStart],
       'Pay Periods Bi-weekly'[PayPeriodEnd],
       'Pay Periods Bi-weekly'[Days Pay Period] )
    )
    [Days Pay Period]
)
parry2k
Super User
Super User

@aashton not sure how the raw data looks like, try this:

 

TotalDaysLastMonth =
SUMX(
    FILTER(
        'Pay Periods Bi-weekly',
        'Pay Periods Bi-weekly'[Check Date Last Month] = 1
    ),
    CALCULATE ( MAX ( 'Pay Periods Bi-weekly'[Days Pay Period] ) )
) 


Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

No, same incorrect answer.  

parry2k
Super User
Super User

@aashton try this:

 

TotalDaysLastMonth =
SUMX(
    FILTER(
        'Pay Periods Bi-weekly',
        'Pay Periods Bi-weekly'[Check Date Last Month] = 1
    ),
    CALCULATE ( SUM ( 'Pay Periods Bi-weekly'[Days Pay Period] ) )
) 


Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

@parry2k  Thanks but I get the same incorrect answer. SEe below, it should be 42, and I get 588:  

aashton_0-1727377355510.png

 

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.