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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
msk_muthu
Regular Visitor

Dax Formula to find out Monthly value from given date range

Hi Team,

Need your help to crack the formula to find out the amount for each month with the given date range

for eg.:
From Date is 05-11-2021,
To date is 06-01-2022,
Invoice amount is 10800,
Total No.of Days are 63,
Per Day Amount is 171,
There will be 12 Calculated column for each month from April to October the value has to reflect 0
for November month value has to reflect for 26 days * per Day Amount
for December month value has to reflect for 31 days * Per Day Amount
for January month value has to reflect for 6 days * Per Day Amount

Please find the attachment and help me to get formulas, Also the date range includes leap year

 

FROM DateTo DateInvoice AmountCC_Activity_No_of_DaysCC_PerDayInvoiceAmountCC_AprilCC_MayCC_JuneCC_JulyCC_AugustCC_SeptemberCC_OctoberCC_NovemberCC_DecemberCC_JanuaryCC_FebruaryCC_MarchTotal
20-Mar-2402-Apr-243,240.0014231.43462.860.000.000.000.000.000.000.000.000.000.002,777.143,240.00
11-Apr-2424-Apr-248,287.9914592.008,287.990.000.000.000.000.000.000.000.000.000.000.008,287.99
05-Nov-2106-Jan-2210,800.0063171.430.000.000.000.000.000.000.004457.145314.291028.570.000.0010,800.00
05-Dec-2201-Mar-23             11,500.0087132.180.000.000.000.000.000.000.000.003,568.974,097.703,701.15132.1811,500.00
15-Nov-2305-Mar-24             18,600.00112166.070.000.000.000.000.000.000.002,657.145,148.215,148.214,816.07830.3618,600.00


Thank you
MSKarthikeyan

1 ACCEPTED SOLUTION
divyed
Resolver IV
Resolver IV

Hello @msk_muthu ,

 

Here is a dax giving same data you want as per sample given. This is dynamic and can run on any data provided.

 

//Idea was to achieve expected output and hence did not focus on formatting and optimization. I know you will do as needed

// This is not the best code honestly but will work on any data in given format. you can create variables and pass as per your need to simplyfy the query 

 

FinalTable =

ADDCOLUMNS(
    Table1,
    "Number_of_days",
        VAR TotalDays = DATEDIFF([From_Date]-1, [To_Date], DAY)
        RETURN TotalDays,

    "Amount_per_Day",
        VAR AmountPerDay = DIVIDE([Invoice_Amount], DATEDIFF([From_Date]-1, [To_Date], DAY), 0)
        RETURN AmountPerDay,
   
    "January",
        VAR StartDateJan = MAX(DATE(YEAR([From_Date]), 1, 1), [From_Date])
        VAR EndDateJan = MIN(DATE(YEAR([From_Date]), 1, 31), [To_Date])
        VAR DaysInJan = DATEDIFF(StartDateJan, EndDateJan, DAY) + 1
        RETURN IF(DaysInJan > 0, ( DaysInJan * DIVIDE([Invoice_Amount], DATEDIFF([From_Date]-1, [To_Date], DAY), 0)), 0),
   
    "February",
        VAR StartDateFeb = MAX(DATE(YEAR([From_Date]), 2, 1), [From_Date])
        VAR EndDateFeb = MIN(DATE(YEAR([From_Date]), 2, 28), [To_Date])
        VAR DaysInFeb = DATEDIFF(StartDateFeb, EndDateFeb, DAY) + 1
        RETURN IF(DaysInFeb > 0, (DaysInFeb * DIVIDE([Invoice_Amount], DATEDIFF([From_Date]-1, [To_Date], DAY), 0)), 0),
   
    "March",
        VAR StartDateMar = MAX(DATE(YEAR([From_Date]), 3, 1), [From_Date])
        VAR EndDateMar = MIN(DATE(YEAR([From_Date]), 3, 31), [To_Date])
        VAR DaysInMar = DATEDIFF(StartDateMar, EndDateMar, DAY) + 1
        RETURN IF(DaysInMar > 0, (DaysInMar * DIVIDE([Invoice_Amount], DATEDIFF([From_Date]-1, [To_Date], DAY), 0)), 0),
   
    "April",
        VAR StartDateApr = MAX(DATE(YEAR([From_Date]), 4, 1), [From_Date])
        VAR EndDateApr = MIN(DATE(YEAR([From_Date]), 4, 30), [To_Date])
        VAR DaysInApr = DATEDIFF(StartDateApr, EndDateApr, DAY) + 1
        RETURN IF(DaysInApr > 0, (DaysInApr * DIVIDE([Invoice_Amount], DATEDIFF([From_Date]-1, [To_Date], DAY), 0)), 0),
   
    "May",
        VAR StartDateMay = MAX(DATE(YEAR([From_Date]), 5, 1), [From_Date])
        VAR EndDateMay = MIN(DATE(YEAR([From_Date]), 5, 31), [To_Date])
        VAR DaysInMay = DATEDIFF(StartDateMay, EndDateMay, DAY) + 1
        RETURN IF(DaysInMay > 0, (DaysInMay * DIVIDE([Invoice_Amount], DATEDIFF([From_Date]-1, [To_Date], DAY), 0)), 0),
   
    "June",
        VAR StartDateJun = MAX(DATE(YEAR([From_Date]), 6, 1), [From_Date])
        VAR EndDateJun = MIN(DATE(YEAR([From_Date]), 6, 30), [To_Date])
        VAR DaysInJun = DATEDIFF(StartDateJun, EndDateJun, DAY) + 1
        RETURN IF(DaysInJun > 0, (DaysInJun * DIVIDE([Invoice_Amount], DATEDIFF([From_Date]-1, [To_Date], DAY), 0)), 0),
   
    "July",
        VAR StartDateJul = MAX(DATE(YEAR([From_Date]), 7, 1), [From_Date])
        VAR EndDateJul = MIN(DATE(YEAR([From_Date]), 7, 31), [To_Date])
        VAR DaysInJul = DATEDIFF(StartDateJul, EndDateJul, DAY) + 1
        RETURN IF(DaysInJul > 0, (DaysInJul * DIVIDE([Invoice_Amount], DATEDIFF([From_Date]-1, [To_Date], DAY), 0)), 0),
   
    "August",
        VAR StartDateAug = MAX(DATE(YEAR([From_Date]), 8, 1), [From_Date])
        VAR EndDateAug = MIN(DATE(YEAR([From_Date]), 8, 31), [To_Date])
        VAR DaysInAug = DATEDIFF(StartDateAug, EndDateAug, DAY) + 1
        RETURN IF(DaysInAug > 0, (DaysInAug * DIVIDE([Invoice_Amount], DATEDIFF([From_Date]-1, [To_Date], DAY), 0)), 0),
   
    "September",
        VAR StartDateSep = MAX(DATE(YEAR([From_Date]), 9, 1), [From_Date])
        VAR EndDateSep = MIN(DATE(YEAR([From_Date]), 9, 30), [To_Date])
        VAR DaysInSep = DATEDIFF(StartDateSep, EndDateSep, DAY) + 1
        RETURN IF(DaysInSep > 0, (DaysInSep * DIVIDE([Invoice_Amount], DATEDIFF([From_Date]-1, [To_Date], DAY), 0)), 0),
   
    "October",
        VAR StartDateOct = MAX(DATE(YEAR([From_Date]), 10, 1), [From_Date])
        VAR EndDateOct = MIN(DATE(YEAR([From_Date]), 10, 31), [To_Date])
        VAR DaysInOct = DATEDIFF(StartDateOct, EndDateOct, DAY) + 1
        RETURN IF(DaysInOct > 0, (DaysInOct * DIVIDE([Invoice_Amount], DATEDIFF([From_Date]-1, [To_Date], DAY), 0)), 0),
   
    "November",
        VAR StartDateNov = MAX(DATE(YEAR([From_Date]), 11, 1), [From_Date])
        VAR EndDateNov = MIN(DATE(YEAR([From_Date]), 11, 30), [To_Date])
        VAR DaysInNov = DATEDIFF(StartDateNov, EndDateNov, DAY) + 1
        RETURN IF(DaysInNov > 0, (DaysInNov * DIVIDE([Invoice_Amount], DATEDIFF([From_Date]-1, [To_Date], DAY), 0)), 0),
   
    "December",
        VAR StartDateDec = MAX(DATE(YEAR([From_Date]), 12, 1), [From_Date])
        VAR EndDateDec = MIN(DATE(YEAR([From_Date]), 12, 31), [To_Date])
        VAR DaysInDec = DATEDIFF(StartDateDec, EndDateDec, DAY) + 1
        RETURN IF(DaysInDec > 0, (DaysInDec * DIVIDE([Invoice_Amount], DATEDIFF([From_Date]-1, [To_Date], DAY), 0)), 0)
)
 
divyed_0-1729600643225.png

Did I answer your query ? Please mark this as solution if this helps. Always love your Kudos 🙂

 

Cheers

https://www.linkedin.com/in/neeraj-kumar-62246b26/

View solution in original post

2 REPLIES 2
divyed
Resolver IV
Resolver IV

Hello @msk_muthu ,

 

Here is a dax giving same data you want as per sample given. This is dynamic and can run on any data provided.

 

//Idea was to achieve expected output and hence did not focus on formatting and optimization. I know you will do as needed

// This is not the best code honestly but will work on any data in given format. you can create variables and pass as per your need to simplyfy the query 

 

FinalTable =

ADDCOLUMNS(
    Table1,
    "Number_of_days",
        VAR TotalDays = DATEDIFF([From_Date]-1, [To_Date], DAY)
        RETURN TotalDays,

    "Amount_per_Day",
        VAR AmountPerDay = DIVIDE([Invoice_Amount], DATEDIFF([From_Date]-1, [To_Date], DAY), 0)
        RETURN AmountPerDay,
   
    "January",
        VAR StartDateJan = MAX(DATE(YEAR([From_Date]), 1, 1), [From_Date])
        VAR EndDateJan = MIN(DATE(YEAR([From_Date]), 1, 31), [To_Date])
        VAR DaysInJan = DATEDIFF(StartDateJan, EndDateJan, DAY) + 1
        RETURN IF(DaysInJan > 0, ( DaysInJan * DIVIDE([Invoice_Amount], DATEDIFF([From_Date]-1, [To_Date], DAY), 0)), 0),
   
    "February",
        VAR StartDateFeb = MAX(DATE(YEAR([From_Date]), 2, 1), [From_Date])
        VAR EndDateFeb = MIN(DATE(YEAR([From_Date]), 2, 28), [To_Date])
        VAR DaysInFeb = DATEDIFF(StartDateFeb, EndDateFeb, DAY) + 1
        RETURN IF(DaysInFeb > 0, (DaysInFeb * DIVIDE([Invoice_Amount], DATEDIFF([From_Date]-1, [To_Date], DAY), 0)), 0),
   
    "March",
        VAR StartDateMar = MAX(DATE(YEAR([From_Date]), 3, 1), [From_Date])
        VAR EndDateMar = MIN(DATE(YEAR([From_Date]), 3, 31), [To_Date])
        VAR DaysInMar = DATEDIFF(StartDateMar, EndDateMar, DAY) + 1
        RETURN IF(DaysInMar > 0, (DaysInMar * DIVIDE([Invoice_Amount], DATEDIFF([From_Date]-1, [To_Date], DAY), 0)), 0),
   
    "April",
        VAR StartDateApr = MAX(DATE(YEAR([From_Date]), 4, 1), [From_Date])
        VAR EndDateApr = MIN(DATE(YEAR([From_Date]), 4, 30), [To_Date])
        VAR DaysInApr = DATEDIFF(StartDateApr, EndDateApr, DAY) + 1
        RETURN IF(DaysInApr > 0, (DaysInApr * DIVIDE([Invoice_Amount], DATEDIFF([From_Date]-1, [To_Date], DAY), 0)), 0),
   
    "May",
        VAR StartDateMay = MAX(DATE(YEAR([From_Date]), 5, 1), [From_Date])
        VAR EndDateMay = MIN(DATE(YEAR([From_Date]), 5, 31), [To_Date])
        VAR DaysInMay = DATEDIFF(StartDateMay, EndDateMay, DAY) + 1
        RETURN IF(DaysInMay > 0, (DaysInMay * DIVIDE([Invoice_Amount], DATEDIFF([From_Date]-1, [To_Date], DAY), 0)), 0),
   
    "June",
        VAR StartDateJun = MAX(DATE(YEAR([From_Date]), 6, 1), [From_Date])
        VAR EndDateJun = MIN(DATE(YEAR([From_Date]), 6, 30), [To_Date])
        VAR DaysInJun = DATEDIFF(StartDateJun, EndDateJun, DAY) + 1
        RETURN IF(DaysInJun > 0, (DaysInJun * DIVIDE([Invoice_Amount], DATEDIFF([From_Date]-1, [To_Date], DAY), 0)), 0),
   
    "July",
        VAR StartDateJul = MAX(DATE(YEAR([From_Date]), 7, 1), [From_Date])
        VAR EndDateJul = MIN(DATE(YEAR([From_Date]), 7, 31), [To_Date])
        VAR DaysInJul = DATEDIFF(StartDateJul, EndDateJul, DAY) + 1
        RETURN IF(DaysInJul > 0, (DaysInJul * DIVIDE([Invoice_Amount], DATEDIFF([From_Date]-1, [To_Date], DAY), 0)), 0),
   
    "August",
        VAR StartDateAug = MAX(DATE(YEAR([From_Date]), 8, 1), [From_Date])
        VAR EndDateAug = MIN(DATE(YEAR([From_Date]), 8, 31), [To_Date])
        VAR DaysInAug = DATEDIFF(StartDateAug, EndDateAug, DAY) + 1
        RETURN IF(DaysInAug > 0, (DaysInAug * DIVIDE([Invoice_Amount], DATEDIFF([From_Date]-1, [To_Date], DAY), 0)), 0),
   
    "September",
        VAR StartDateSep = MAX(DATE(YEAR([From_Date]), 9, 1), [From_Date])
        VAR EndDateSep = MIN(DATE(YEAR([From_Date]), 9, 30), [To_Date])
        VAR DaysInSep = DATEDIFF(StartDateSep, EndDateSep, DAY) + 1
        RETURN IF(DaysInSep > 0, (DaysInSep * DIVIDE([Invoice_Amount], DATEDIFF([From_Date]-1, [To_Date], DAY), 0)), 0),
   
    "October",
        VAR StartDateOct = MAX(DATE(YEAR([From_Date]), 10, 1), [From_Date])
        VAR EndDateOct = MIN(DATE(YEAR([From_Date]), 10, 31), [To_Date])
        VAR DaysInOct = DATEDIFF(StartDateOct, EndDateOct, DAY) + 1
        RETURN IF(DaysInOct > 0, (DaysInOct * DIVIDE([Invoice_Amount], DATEDIFF([From_Date]-1, [To_Date], DAY), 0)), 0),
   
    "November",
        VAR StartDateNov = MAX(DATE(YEAR([From_Date]), 11, 1), [From_Date])
        VAR EndDateNov = MIN(DATE(YEAR([From_Date]), 11, 30), [To_Date])
        VAR DaysInNov = DATEDIFF(StartDateNov, EndDateNov, DAY) + 1
        RETURN IF(DaysInNov > 0, (DaysInNov * DIVIDE([Invoice_Amount], DATEDIFF([From_Date]-1, [To_Date], DAY), 0)), 0),
   
    "December",
        VAR StartDateDec = MAX(DATE(YEAR([From_Date]), 12, 1), [From_Date])
        VAR EndDateDec = MIN(DATE(YEAR([From_Date]), 12, 31), [To_Date])
        VAR DaysInDec = DATEDIFF(StartDateDec, EndDateDec, DAY) + 1
        RETURN IF(DaysInDec > 0, (DaysInDec * DIVIDE([Invoice_Amount], DATEDIFF([From_Date]-1, [To_Date], DAY), 0)), 0)
)
 
divyed_0-1729600643225.png

Did I answer your query ? Please mark this as solution if this helps. Always love your Kudos 🙂

 

Cheers

https://www.linkedin.com/in/neeraj-kumar-62246b26/

sanalytics
Solution Sage
Solution Sage

@msk_muthu 

 

Can you provide some more aspect with some dummy data and your exact output and the formula concept.So that we can help you to build the DAX formula.

 

Regards

sanalytics

If it is your solution then please like and accept it as solution

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.