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
Alexandra_B
Helper III
Helper III

Calculate sum of specific months

Hello.

 

I need to calculate the following formula, but based on specific months (ie April to September 2021, 2022)

 

x = number + number + sum(data in a table). I think I need to use DATESINPERIOD. but I don`t know how to select the months.

I have a date table (01/01/2019 to present, I will need April to Sept 21 and 22)

 

How could I do it?

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

Hi @Alexandra_B ,

According to your description, I create a sample.

vkalyjmsft_0-1657373693483.png

Here's my solution, create a measure.

X =
VAR _Sum =
    CALCULATE (
        SUM ( 'BB_EGLF_CDD_15 5C_till_june22'[CDD 15.5] ),
        FILTER (
            ALL ( 'BB_EGLF_CDD_15 5C_till_june22' ),
            MONTH ( 'BB_EGLF_CDD_15 5C_till_june22'[Month] )
                IN { 4, 5, 6, 7, 8, 9 }
                    && YEAR ( 'BB_EGLF_CDD_15 5C_till_june22'[Month] ) IN { 2019, 2020, 2021, 2022 }
        )
    )
RETURN
    42180.2942 + 68.8235277 * _Sum

Here I only return the sum and get the correct result.

vkalyjmsft_1-1657374121830.png

I attach my sample below for reference.

 

Best Regards,
Community Support Team _ kalyj

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

View solution in original post

7 REPLIES 7
v-yanjiang-msft
Community Support
Community Support

Hi @Alexandra_B ,

According to your description, I create a sample.

vkalyjmsft_0-1657373693483.png

Here's my solution, create a measure.

X =
VAR _Sum =
    CALCULATE (
        SUM ( 'BB_EGLF_CDD_15 5C_till_june22'[CDD 15.5] ),
        FILTER (
            ALL ( 'BB_EGLF_CDD_15 5C_till_june22' ),
            MONTH ( 'BB_EGLF_CDD_15 5C_till_june22'[Month] )
                IN { 4, 5, 6, 7, 8, 9 }
                    && YEAR ( 'BB_EGLF_CDD_15 5C_till_june22'[Month] ) IN { 2019, 2020, 2021, 2022 }
        )
    )
RETURN
    42180.2942 + 68.8235277 * _Sum

Here I only return the sum and get the correct result.

vkalyjmsft_1-1657374121830.png

I attach my sample below for reference.

 

Best Regards,
Community Support Team _ kalyj

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

Thank you 🙂

Anonymous
Not applicable

@Alexandra_B Try this 

var a = calculate ( sum (data in table ) , DATESBETWEEN ( your date column , Start date , End date))
return number + number + a

Hello, @Anonymous .
Thank you for your reply.
I have tried, but I get the following error.

The syntax for 'return' is incorrect.
(DAX(CALCULATE (SUM( 'BB_EGLF_CDD_15 5C_till_june22'[CDD 15.5] ), DATESBETWEEN('BB_EGLF_CDD_15 5C_till_june22'[Month], DATE(2021,04,01), DATE(2021,09,30)))return "42180.2942" + "68.8235277" * 'a')).

I am doing it wrong?

Anonymous
Not applicable

Create a measure 
x = 
var a =CALCULATE (SUM( 'BB_EGLF_CDD_15 5C_till_june22'[CDD 15.5] ),
DATESBETWEEN('BB_EGLF_CDD_15 5C_till_june22'[Month],
DATE(2021,04,01),
DATE(2021,09,30))
return 42180.2942 + 68.8235277 * a

What if I need for all the years same months , 2019 - 2022? That means I cannot use DATESBETWEEN for it... 😞

Anonymous
Not applicable

yes correct, Datesbetween wont work there. 
can you show what exactly you are trying to achive.

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!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

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.