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

Help! Calculate the distinct positions considering the last inspection in the month the vehicle

Hello all,

I need help, I'm not able to calculate the distinct positions considering the last inspection of the vehicle in the month.

 

TABLE:

FleetPositionDateValue
Fleet1    695475-XC    2020-04-05    6.8
Fleet1695475-XC2020-04-1610.2
Fleet1324567-KQ2020-04-257.4
Fleet1324567-KQ2020-04-1215.5
Fleet1893765-JD2020-04-198.9
Fleet1893765-JD2020-04-285.2

 

FILTERS: Year: 2020 | Month: April
RESULT

 

Fleet  Total Unique Positions -
Over (10) Qty
Total Unique Positions - 
Between (7-10) Qty
Total Unique Positions - 
Below (7) Qty
Fleet1    111

 

I'll be very grateful if anyone can help me!

Regards,
Alexandre Martins

1 ACCEPTED SOLUTION
alexandrem79
Frequent Visitor

I was able to prepare a solution after watching the video on the link below:
Calculate The Last Or Max Value Within A Table Of Data Using DAX In Power BI

Solution:

Measure Max Date Position =
VAR CurrentPosition = SELECTEDVALUE(inspections[Position])

RETURN
MAXX(
FILTER( ALLSELECTED(inspections), inspections[Position] = CurrentPosition),
inspections[Date] )
)

Measure Over 10 =
CALCULATE(
DISTINCTCOUNT(inspections[Position]),
inspections[Value] > 10,
FILTER(inspections, inspections[Date] = [Max Date Position])
)

Measure Between 7-10 =
CALCULATE(
DISTINCTCOUNT(inspections[Position]),
inspections[Value] >= 7,
inspections[Value] <= 10,
FILTER(inspections, inspections[Date] = [Max Date Position])
)

Measure Below 7 =
CALCULATE(
DISTINCTCOUNT(inspections[Position]),
inspections[Value] < 7,
FILTER(inspections, inspections[Date] = [Max Date Position])
)


Thank you all,
Regards!

View solution in original post

6 REPLIES 6
alexandrem79
Frequent Visitor

I was able to prepare a solution after watching the video on the link below:
Calculate The Last Or Max Value Within A Table Of Data Using DAX In Power BI

Solution:

Measure Max Date Position =
VAR CurrentPosition = SELECTEDVALUE(inspections[Position])

RETURN
MAXX(
FILTER( ALLSELECTED(inspections), inspections[Position] = CurrentPosition),
inspections[Date] )
)

Measure Over 10 =
CALCULATE(
DISTINCTCOUNT(inspections[Position]),
inspections[Value] > 10,
FILTER(inspections, inspections[Date] = [Max Date Position])
)

Measure Between 7-10 =
CALCULATE(
DISTINCTCOUNT(inspections[Position]),
inspections[Value] >= 7,
inspections[Value] <= 10,
FILTER(inspections, inspections[Date] = [Max Date Position])
)

Measure Below 7 =
CALCULATE(
DISTINCTCOUNT(inspections[Position]),
inspections[Value] < 7,
FILTER(inspections, inspections[Date] = [Max Date Position])
)


Thank you all,
Regards!

v-yingjl
Community Support
Community Support

Hi @alexandrem79 ,

I have created a calculated table to achieve this:

Table = 
SUMMARIZE (
    'inspections',
    'inspections'[Fleet],
    'inspections'[Position],
    "last date", MAX ( 'inspections'[Date] ),
    "Value",
    VAR position = [Position]
    RETURN
        CALCULATE (
            SUM ( inspections[Value] ),
            FILTER (
                ALL ( inspections ),
                inspections[Position] = position
                    && inspections[Date]
                        = CALCULATE (
                            MAX ( inspections[Date] ),
                            FILTER ( ALL ( inspections ), inspections[Position] = position )
                        )
            )
        )
)

Three measures to calculate conditional totals:

Total Unique Positions -Over (10) Qty = 
CALCULATE ( DISTINCTCOUNT ( 'Table'[Value] ), 'Table'[Value] > 10 )
Total Unique Positions - Between (7-10) Qty = 
CALCULATE (
    DISTINCTCOUNT ( 'Table'[Value] ),
    'Table'[Value] >= 7
        && 'Table'[Value] <= 10
)
Total Unique Positions - Below (7) Qty = 
CALCULATE ( DISTINCTCOUNT ( 'Table'[Value] ), 'Table'[Value] < 7 )

result.png

 

Sample file is attached that hopes to help you, please check and try it: Calculate the distinct positions considering the last inspection in the month the vehicle.pbix 

 

Best Regards,
Yingjie Li

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

Ashish_Mathur
Super User
Super User

Hi,

How have you got those answers - 1 for each?  Shouldn't the answer of Total Unique Positions - Over (10) Qty be 2?


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Thanks for your answer!

I need the value of the most recent inspection of the month:

 

FleetPositionDateValue
Fleet1 695475-XC2020-04-056.8
Fleet1    695475-XC2020-04-1610.2

 

FleetTotal Unique Positions -
Over (10) Qty
Total Unique Positions - 
Between (7-10) Qty
Total Unique Positions - 
Below (7) Qty
Fleet1    100

 

I tried that, but it did not work:

Total Unique Positions - Over (10) Qty =

CALCULATE(
DISTINCTCOUNT(inspections[Position]),
FILTER(inspections, inspections[Value] > 10),
FILTER(inspections, inspections[Date] = MAX(inspections[Date]))
)
 
It's calculating the last date of the table and ignoring the position of the vehicle.

@alexandrem79 , Please find the attached solution file. You may have to change no of groupbys in summarize as per need.

 

The measure is the latest. and measure 2 is the latest in month

 

 

 

Hi,

The most recent inspection for the month is 16 April 2020 for 695475-XC and 12 April 2020 for 324567-KQ.  The value for both such instances is > 10 so thae answer should be 2.  You may download my PBI file from here.

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

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.