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
CM_Mills
Helper II
Helper II

Value Based on Max Date Per Month

Hello,

 

I'm looking to create a measure to create a new monthly value but only on the latest date or the last date of a month. The rest of the dates I require the value to be 0.

 

This needs to be done per a product highlighted by a 'code'.

 

The dates i have linked in another table.

 

The exmple below is taken as if it's the 11th February. The  'monthly value' column I need to create would need 0 in the date for all January execpt the last date there is a value for that Code. So for ABC that would be 17 & XYZ that would be 25. However they would have different last dates. The 31st Jan for ABC and 30th Jan for XYZ.

 

For February in the example if it part way through the month i require it to show the latest date. So for ABC and XYZ that would be both 10th February and ABC is 16 and XYZ is 55.

 

TABLE 1 TABLE 2
Date Daily ValueMonthly ValueCode
26/01/2020 170ABC
27/01/2020 150ABC
28/01/2020 140ABC
29/01/2020 150ABC
30/01/2020 160ABC
31/01/2020 1717ABC
01/02/2020 200ABC
02/02/2020 210ABC
03/02/2020 220ABC
04/02/2020 220ABC
05/02/2020 220ABC
06/02/2020 180ABC
07/02/2020 190ABC
08/02/2020 100ABC
09/02/2020 150ABC
10/02/2020 1616ABC
26/01/2020 170XYX
27/01/2020 150XYX
28/01/2020 140XYX
29/01/2020 200XYX
30/01/2020 2525XYX
01/02/2020 200XYX
02/02/2020 210XYX
03/02/2020 350XYX
04/02/2020 380XYX
05/02/2020 400XYX
06/02/2020 350XYX
07/02/2020 420XYX
08/02/2020 450XYX
09/02/2020 500XYX
10/02/2020 5555XYX

 

I would really appricate any help writing the measure for this as i'm pretty stuck trying to work it out.

 

Many Thanks

 

 

 

 

1 ACCEPTED SOLUTION

Hi @CM_Mills ,

 

Do you mean there is no data of 3/28 in your data table? If so, try to use the following measure:

 

Open Interest - Lots (Monthly) =
IF (
    MAX ( 'Date - Trade Calendar'[Date - Trade] ) IN VALUES ( Data[Date] ),
    IF (
        MAX ( 'Date - Trade Calendar'[Date - Trade] )
            = CALCULATE (
                LASTNONBLANK ( Data[Date - Trade], SUM ( Data[Open Interest - Lots] ) ),
                FILTER (
                    ALL ( Data ),
                    Data[Unique Identifier] = MAX ( Data[Unique Identifier] )
                        && MONTH ( Data[Date - Trade] ) = MONTH ( MAX ( Data[Date - Trade] ) )
                )
            ),
        CALCULATE (
            LASTNONBLANKVALUE ( 'Data'[Date - Trade], SUM ( Data[Open Interest - Lots] ) ),
            FILTER (
                ALL ( Data ),
                Data[Unique Identifier] = MAX ( Data[Unique Identifier] )
                    && MONTH ( Data[Date - Trade] ) = MONTH ( MAX ( Data[Date - Trade] ) )
            )
        ),
        0
    ),
    BLANK ()
)

 

 

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

 

Best Regards,

Dedmon Dai

 

View solution in original post

8 REPLIES 8
amitchandak
Super User
Super User

@CM_Mills , Try a measure like

calculate(lastnonblankvalue(Table[date], sum(Table[Value])), filter(allselected(Table), eomonth(Table[date],0) = eomonth(max(Table[date]),0)))

 

Please provide your feedback comments and advice for new videos
Tutorial Series Dax Vs SQL Direct Query PBI Tips
Appreciate your Kudos.

Thank you for reviewing

Hi @CM_Mills ,

 

What's the relationship between your table1 and table2? How do you link these two tables? Would you please show us complete sample data for table1 and table2?

 

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

 

Best Regards,

Dedmon Dai

Table 1 is called: Date - Trade Calendar

Table 2 is called: Data

 

Date - Trade Calendar is linked by Date to Data: One to many

 

The my data poitns are called:

 

Date - Trade Calendar

Date - Trade

 

Data

Open Interest (lots)   

Unique Identifier

 

 

The dates are formatted as dates, open interest is a number and unique identifer is a text. Unfortuantlty i can't share the data. Thank you for your help

Hi @CM_Mills ,

 

Would you please try the following measure:

 

 

Monthly Value = IF(MAX('Date'[Date])= CALCULATE(LASTNONBLANK(Data[Date],SUM(Data[Daily Value])),FILTER(ALL(Data),Data[Code] = MAX(Data[Code])&&MONTH(Data[Date])= MONTH(MAX(Data[Date])))),CALCULATE(LASTNONBLANKVALUE('Data'[Date],SUM(Data[Daily Value])),FILTER(ALL(Data),Data[Code] = MAX(Data[Code])&&MONTH(Data[Date])= MONTH(MAX(Data[Date])))),0)

 

Capture1.PNG

For more details, please refer to the pbix file: https://qiuyunus-my.sharepoint.com/:u:/g/personal/pbipro_qiuyunus_onmicrosoft_com/EeEjelqlkJVEp39uEd...

 

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

 

Best Regards,

Dedmon Dai

 

 

Hi,

 

Thank you this, it's nearly there. The formula has now made extra rows show where there is no data. For example previously there was no 28th March shown but now in the table it shoes that date with BLANK value in the orginial data point (Open Interest - Lots) and a 0 in the new measure.

It would work better if the measure didn't add a value to these dates so they don't show.

 

Any help provided would be truely appricated:

Open Interest - Lots (Monthly) = (IF(MAX('Date - Trade Calendar'[Date - Trade])= CALCULATE(LASTNONBLANK(Data[Date - Trade],SUM(Data[Open Interest - Lots])),FILTER(ALL(Data),Data[Unique Identifier] = MAX(Data[Unique Identifier])&&MONTH(Data[Date - Trade])= MONTH(MAX(Data[Date - Trade])))),CALCULATE(LASTNONBLANKVALUE('Data'[Date - Trade],SUM(Data[Open Interest - Lots])),FILTER(ALL(Data),Data[Unique Identifier] = MAX(Data[Unique Identifier])&&MONTH(Data[Date - Trade])= MONTH(MAX(Data[Date - Trade])))),0))

Hi @CM_Mills ,

 

Do you mean there is no data of 3/28 in your data table? If so, try to use the following measure:

 

Open Interest - Lots (Monthly) =
IF (
    MAX ( 'Date - Trade Calendar'[Date - Trade] ) IN VALUES ( Data[Date] ),
    IF (
        MAX ( 'Date - Trade Calendar'[Date - Trade] )
            = CALCULATE (
                LASTNONBLANK ( Data[Date - Trade], SUM ( Data[Open Interest - Lots] ) ),
                FILTER (
                    ALL ( Data ),
                    Data[Unique Identifier] = MAX ( Data[Unique Identifier] )
                        && MONTH ( Data[Date - Trade] ) = MONTH ( MAX ( Data[Date - Trade] ) )
                )
            ),
        CALCULATE (
            LASTNONBLANKVALUE ( 'Data'[Date - Trade], SUM ( Data[Open Interest - Lots] ) ),
            FILTER (
                ALL ( Data ),
                Data[Unique Identifier] = MAX ( Data[Unique Identifier] )
                    && MONTH ( Data[Date - Trade] ) = MONTH ( MAX ( Data[Date - Trade] ) )
            )
        ),
        0
    ),
    BLANK ()
)

 

 

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

 

Best Regards,

Dedmon Dai

 

Would that solution take into account the code?

 

I have tried to enter the measure but I'm getting the error: 

 

A single value for column 'Date - Trade' in table 'Date - Trade Calendar' cannot be determined. This can happen when a measure formula refers to a column that contains many values without specifying an aggregation such as min, max, count, or sum to get a single result.

 

Please see my measure:

 

Measure = calculate(lastnonblankvalue('Date - Trade Calendar'[Date - Trade], sum(Data[Open Interest - Lots])), filter(allselected(Data), eomonth('Date - Trade Calendar'[Date - Trade],0) = eomonth(max('Date - Trade Calendar'[Date - Trade]),0)))
 
 
 

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.