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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Anonymous
Not applicable

Measure to find sum between date ranges in another table

Hi All,

 

I would appreciate some help with the following..

 

I have three tables:

 

Table1:

StoreIDBeginEnd
X1-1-20243-1-2024
X5-1-2024 
Y2-1-20245-1-2024
Y7-1-20248-1-2024
Z1-1-20244-1-2024

 

Table2:

 

StoreIDDateSoldAmount
X1-1-20241
X3-1-20242
X8-1-20241
Y2-1-20243
Y6-1-20241
Z1-1-20242
Z7-1-20242

 

Table3 is just a table with unique Store ID's to create a connection between Table1 and Table2.

 

What I want to achieve is a total sum of Table2[Amount] where the Table2[DateSold] is between a period of Table1[Begin] and Table1[End] (or that Table1[End] is empty). Table1[Begin] is included and Table1[End] is excluded in the date range.

So for example, for StoreID = 'X', we have three transactions:

 

The transaction of 1-1-2024 with Amount = 1 falls within the period 1-1-2024 until 3-1-2024.
The transaction of 3-1-2024 with Amount = 2 does NOT fall within the period 1-1-2024 until 3-1-2024 (and is not after 5-1-2024).
The transaction of 8-1-2024 with Amount = 1 falls within the period 5-1-2024 until [empty].

For StoreID = 'X', the total of the new measure should be: 2.

 

Similarly:

 

For StoreID = 'Y', the total of the new measure should be: 3.

For StoreID = 'Z', the total of the new measure should be: 2.

 

In the end, I want to show a table like this:

 

StoreIDMeasure
X2
Y3
Z2
Total7

 

How should I write a measure for this?

I tried a SUMX with filter to account for the date ranges but that measure only shows correct values when I have a table that includes the columns of Table1. Also, the total at the bottom row shows an incorrect value.

Thanks for your attention! 🙂

 

 

1 ACCEPTED SOLUTION
MFelix
Super User
Super User

Hi @Anonymous ,

 

Add the following two measures:

 

Sales in Period =
SUMX (
    ADDCOLUMNS (
        'Table',
        "SalesInPeridod",
            IF (
                'Table'[End] <> BLANK (),
                CALCULATE (
                    SUM ( 'Table (2)'[Amount] ),
                    FILTER (
                        'Table (2)',
                        'Table (2)'[DateSold] >= 'Table'[Begin]
                            && 'Table (2)'[DateSold] < 'Table'[End]
                    )
                ),
                CALCULATE (
                    SUM ( 'Table (2)'[Amount] ),
                    FILTER ( 'Table (2)', 'Table (2)'[DateSold] >= 'Table'[Begin] )
                )
            )
    ),
    [SalesInPeridod]
)


Total Sales In Period = SUMX('Table (3)', [Sales in Period])

 

The use the last measure for your visualization.

 

Spliting this into two measure to simplify the calculation.

 

MFelix_1-1704804881365.png

 

MFelix_0-1704803092821.png

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



View solution in original post

3 REPLIES 3
Anonymous
Not applicable

Hi  @Anonymous , 
@MFelix nice method!And here's my solution.

1.Use power query to merge table1 and table 2

vheqmsft_0-1704851704057.png

2.Expand table2 column

vheqmsft_1-1704851783858.png

3.Close and apply and create a measure

Measure = 
CALCULATE(
    SUM(Table1[Table2.Amount]),
    FILTER(
        Table1,
        (Table1[End] <> BLANK() && Table1[Table2.DateSold] >= Table1[Begin] && Table1[Table2.DateSold] < Table1[End])
        ||
        (Table1[End] = BLANK() && Table1[Table2.DateSold] >= Table1[Begin])
)
)

4.Final output

 vheqmsft_2-1704852000715.png

vheqmsft_4-1704852229038.png

 

Best regards

Albert He

 

 

Hi @Anonymous ,

 

This bring the problem of getting repeated information, in this case you can add on the PQ a flag column that checks if the value is within the time frame and then filter out the information and just make a simple sum, no need for complex dax.


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



MFelix
Super User
Super User

Hi @Anonymous ,

 

Add the following two measures:

 

Sales in Period =
SUMX (
    ADDCOLUMNS (
        'Table',
        "SalesInPeridod",
            IF (
                'Table'[End] <> BLANK (),
                CALCULATE (
                    SUM ( 'Table (2)'[Amount] ),
                    FILTER (
                        'Table (2)',
                        'Table (2)'[DateSold] >= 'Table'[Begin]
                            && 'Table (2)'[DateSold] < 'Table'[End]
                    )
                ),
                CALCULATE (
                    SUM ( 'Table (2)'[Amount] ),
                    FILTER ( 'Table (2)', 'Table (2)'[DateSold] >= 'Table'[Begin] )
                )
            )
    ),
    [SalesInPeridod]
)


Total Sales In Period = SUMX('Table (3)', [Sales in Period])

 

The use the last measure for your visualization.

 

Spliting this into two measure to simplify the calculation.

 

MFelix_1-1704804881365.png

 

MFelix_0-1704803092821.png

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

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.

Top Solution Authors