cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Victormar
Helper II
Helper II

How to create a measure with a reference column as a date filter

Hi all,

 

I would like to create different measures with an If clause referred to a date.

 

I have a list of vehicles with a contract start date, and I am calculating measures like average speed, energy consumption, etc, for every year of the contract.

My idea would be to calculate the measures for every year only if a full year contract has been achieved, something like:

 

Net Energy Consumption (KWh) 1st year =

IF (

CONTRACT[START_DATE] + ( 365.25 * 1 )
< TODAY (),
VAR reading =

CALCULATETABLE (

VALUES ( READINGS[Reading_Id] ),
FILTER (

READINGS,
READINGS[Timestamp] < CONTRACT[START_DATE] + ( 365.25 * 1 )

&& READINGS[Timestamp] > CONTRACT[START_DATE]

)

)
VAR energy =

CALCULATE (

SUM ( 'READINGS'[E1] )

- SUM ( 'READINGS'[E2] )
+ SUM ( 'READINGS'[E3] )
+ SUM ( 'READINGS'[E4] ),

READINGS[Reading_Id] IN reading
)

RETURN

energy,

BLANK ()

)

 

I want the different reading I have for the different values to be in the years of the contract, not our natural year (jan-dec), and the date being after the START_DATE. Also I want to apply this calculation for the coming years, this is why I add a "1", to reuse the formula (although I know it's not so elegant).

 

My main problem is that I cannot reference the column START_DATE in a measure.

 

Thanks in advance 🙂

1 ACCEPTED SOLUTION

Hi @Victormar ,

If you want to get the max mileage of each vehicle_ID within the contract_start_date after adding one year, here's my solution.

1.Create relationship between the two tables with the vehicle_ID column.

2.Create a measure.

Max =
MAXX (
    FILTER (
        ALL ( 'Mileage Table' ),
        'Mileage Table'[READOUT_DATE]
            <= DATE ( YEAR ( MAX ( 'Contract Table'[CONTRACT_START_DATE] ) ) + 1, MONTH ( MAX ( 'Contract Table'[CONTRACT_START_DATE] ) ), DAY ( MAX ( 'Contract Table'[CONTRACT_START_DATE] ) ) )
            && 'Mileage Table'[VEHICLE_ID] = MAX ( 'Mileage Table'[VEHICLE_ID] )
    ),
    'Mileage Table'[MILEAGE]
)

Put the vehicle_ID from the contract table and the measure in a visual, get the result.

vkalyjmsft_0-1653386534980.png

I attach my sample below for reference.

 

Best Regards,
Community Support Team _ kalyj

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

 

View solution in original post

4 REPLIES 4
Victormar
Helper II
Helper II

thanks to all!!! 🙂

 

amitchandak
Super User
Super User

@Victormar ,
Can you share sample data and sample output in table format? Or a sample pbix after removing sensitive data.

 

But I think the approach in these two blogs should help

https://community.powerbi.com/t5/Community-Blog/How-to-divide-distribute-values-between-start-date-o...

https://community.powerbi.com/t5/Community-Blog/HR-Analytics-Active-Employee-Hire-and-Termination-tr...



Power BI Features || Datamarts: https://youtu.be/8tskWsJTEpg || Field Parameters : https://youtu.be/lqF3Wa1FllE?t=70
Time Intelligence Decoded : https://youtu.be/aU2aKbnHuWs&t=145s
Did I answer your question? Mark my post as a solution! Appreciate your Kudos !! Proud to be a Super User! !!
Dashboard of My Blogs !! Connect on Linkedin !! Subscribe to my youtube Channel !!
Want To Learn Power BI | Beginners !! Advance Concepts !! Power BI For Tableau User !!

I have been thinking about it, and it might serve my purpose to be able to get the maximum value of a column (mileage) for each vehicle within the range of the first perior of their contract, which is in another table (contracts table has the starting date of the contract, and I would add 1 year to it):

CONTRACT TABLE  MILEAGE TABLE  
VEHICLE_IDCONTRACT_START_DATE VEHICLE_IDREADOUT_DATEMILEAGE
101/01/2021 101/09/202130000
202/02/2021 105/01/202120000
304/01/2021 102/02/202270000
403/05/2019 101/04/202125000
504/01/2018 203/04/202240000
   205/02/20211000
   206/06/202110000
   207/11/202125000
   307/01/20211000
   306/04/202110000
   306/03/202235000

 

Exemple attached of what I want


Thanks again 🙂

Hi @Victormar ,

If you want to get the max mileage of each vehicle_ID within the contract_start_date after adding one year, here's my solution.

1.Create relationship between the two tables with the vehicle_ID column.

2.Create a measure.

Max =
MAXX (
    FILTER (
        ALL ( 'Mileage Table' ),
        'Mileage Table'[READOUT_DATE]
            <= DATE ( YEAR ( MAX ( 'Contract Table'[CONTRACT_START_DATE] ) ) + 1, MONTH ( MAX ( 'Contract Table'[CONTRACT_START_DATE] ) ), DAY ( MAX ( 'Contract Table'[CONTRACT_START_DATE] ) ) )
            && 'Mileage Table'[VEHICLE_ID] = MAX ( 'Mileage Table'[VEHICLE_ID] )
    ),
    'Mileage Table'[MILEAGE]
)

Put the vehicle_ID from the contract table and the measure in a visual, get the result.

vkalyjmsft_0-1653386534980.png

I attach my sample below for reference.

 

Best Regards,
Community Support Team _ kalyj

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

 

Helpful resources

Announcements
August 1 episode 9_no_dates 768x460.jpg

The Power BI Community Show

Watch the playback when Priya Sathy and Charles Webb discuss Datamarts! Kelly also shares Power BI Community updates.

Power BI Dev Camp Session 24 without aka link and time 768x460.jpg

Ted's Dev Camp - July 28, 2022

Watch Session 24 of Ted's Dev Camp along with past sessions!

Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Top Solution Authors
Top Kudoed Authors