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
Andyr
Frequent Visitor

Is this possible - lookup and iteration adding numbers in a calculated column

I've got a model with two tables, one with records of when a mechanical component was used and one with dates of the last time that component was serviced.

 

ComponentData:

  • PartId
  • Date used
  • Value Produced

ComponentServiceDates

  • PartId
  • DateOfLastService

I would like to know if it is possible and if so what is the best way to achieve add a calculated column? to the ComponentData table that has the number of uses since the last service.

 

Does anyone know if this is actually possible?

 

I'm assuming as a first stage I need to create a lookup column that gets the most recent DateOfLastService value ComponentServiceDates for the specific PartId.

 

An example of data shape that I require would be:

 

PartId, Date used, value Produced, DateOfLastService, UsesSinceService
1, 2016-02-01, 65, 2016-02-01, 1
1, 2016-02-02, 55, 2016-02-01, 2
1, 2016-02-03, 45, 2016-02-01, 3
1, 2016-02-08, 35, 2016-02-01, 4
1, 2016-02-09, 25, 2016-02-01, 5
1, 2016-02-12, 65, 2016-02-12, 1
1, 2016-02-15, 55, 2016-02-12, 2
2, 2016-01-20, 65, 2016-01-12, 1
2, 2016-02-02, 55, 2016-02-01, 2

 

Any help would be more than welcome, I'm totally new to DAX and power query!

 

Thanks again

1 ACCEPTED SOLUTION
v-haibl-msft
Employee
Employee

@Andyr

 

If you have two tables like below, you can create two columns of LastServiceDate and UsesSinceService in ComponentData table. Then you should be able to draw the graph you wanted.

Is this possible - lookup and iteration adding numbers in a calculated column_1.jpg

LastServiceDate = 
CALCULATE (
    MAX ( ComponentServiceDates[DateOfLastService] ),
    FILTER (
        ComponentServiceDates,
        ComponentServiceDates[PartId] = ComponentData[PartId]
            && ComponentServiceDates[DateOfLastService] <= ComponentData[Date used]
    )
)
UsesSinceService = 
CALCULATE (
    COUNTROWS ( ComponentData ),
    FILTER (
        ComponentData,
        ComponentData[Date used] <= EARLIER ( ComponentData[Date used] )
            && ComponentData[PartId] = EARLIER ( ComponentData[PartId] )
            && ComponentData[LastServiceDate] = EARLIER ( ComponentData[LastServiceDate] )
    )
)

Is this possible - lookup and iteration adding numbers in a calculated column_2.jpg

 

Best Regards,

Herbert

View solution in original post

3 REPLIES 3
v-haibl-msft
Employee
Employee

@Andyr

 

If you have two tables like below, you can create two columns of LastServiceDate and UsesSinceService in ComponentData table. Then you should be able to draw the graph you wanted.

Is this possible - lookup and iteration adding numbers in a calculated column_1.jpg

LastServiceDate = 
CALCULATE (
    MAX ( ComponentServiceDates[DateOfLastService] ),
    FILTER (
        ComponentServiceDates,
        ComponentServiceDates[PartId] = ComponentData[PartId]
            && ComponentServiceDates[DateOfLastService] <= ComponentData[Date used]
    )
)
UsesSinceService = 
CALCULATE (
    COUNTROWS ( ComponentData ),
    FILTER (
        ComponentData,
        ComponentData[Date used] <= EARLIER ( ComponentData[Date used] )
            && ComponentData[PartId] = EARLIER ( ComponentData[PartId] )
            && ComponentData[LastServiceDate] = EARLIER ( ComponentData[LastServiceDate] )
    )
)

Is this possible - lookup and iteration adding numbers in a calculated column_2.jpg

 

Best Regards,

Herbert

It is possible, and also normal to *think* this is what you need. But it is not what you should do. Have a read of this article I wrote on the topic. http://exceleratorbi.com.au/calculated-columns-vs-measures-dax/

 

So so what is the end state of what you are trying to do?  Regardless, you should create a new lookup table that has all the components. It sounds to me like your tabkes are both data tabkes. Read about connecting multiple data tables here http://exceleratorbi.com.au/multiple-data-tables-power-pivot/

 

then in post back with info about your end state for further advice. 



* Matt is an 8 times Microsoft MVP (Power BI) and author of the Power BI Book Supercharge Power BI.

Hi Matt,

Thanks for the reply, 

 

I did state that I wanted to know what the best way to do what I want was, that was what I was asking - maybe I should not have writen what I though the solution may be - I am happy to be corrected as I am completely new to this and from a programming and sql background not excel.

The end result of what I was trying to do was to have a graph with a line for each Component with the value at use on the y-axis vs the number of uses on the x-axis to see degradation over uses. 


The above approach has been scraped - the number of uses is now being brough out of the database directly so I no longer need to shape the data in the way I was asking about.

Thanks again
 




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.