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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
LilianYip
Regular Visitor

(ADD COLUMN) MAX VALUE MULTIPLE CONDITIONS

Hi, would need help on creating "Add Column"  for the current senario.

To choose the highest Delivery charge if the Clinic Name delivered by same driver in the same day.

 

Posting_DateSales_Invoice_NoClinic NameDriver Delivery Charge**Desired Delivery ChargeNote
1/4/2024MISCDEL-8323Clinic ADriver A55 
1/4/2024SI-24-031752Clinic BDriver A5  
1/4/2024SI-24-031768Clinic CDriver B55 
1/4/2024SI-24-031774Clinic BDriver A1010** Choose the highest fee in the date for same clinic
1/4/2024SI-24-031829Clinic DDriver B1010 
2/4/2024SI-24-031864Clinic EDriver B55 
2/4/2024SI-24-031893Clinic CDriver B1010 
2/4/2024SI-24-031895Clinic FDriver A55** Only Count 1 trip
2/4/2024SI-24-031923Clinic FDriver A5  
1 ACCEPTED SOLUTION

Hi @LilianYip 

 

Please try this:

You can create a measure like this:

MEASURE =
VAR _currentdate =
    MAX ( 'Table'[Posting_Date] )
VAR _currentClinic =
    SELECTEDVALUE ( 'Table'[Clinic Name] )
RETURN
    CALCULATE (
        MAX ( 'Table'[Delivery Charge] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[Clinic Name] = _currentClinic
                && 'Table'[Posting_Date] = _currentdate
        )
    )

The result is as follow:

vzhengdxumsft_1-1714617885302.png

 

Best Regards

Zhengdong Xu
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

5 REPLIES 5
v-zhengdxu-msft
Community Support
Community Support

Hi @LilianYip 

 

Thanks for the reply from @lbendlin , please allow me to provide another insight:

I add a calculated column:

Desired Charge =
IF (
    'Table'[Delivery Charge]
        = CALCULATE (
            MAX ( 'Table'[Delivery Charge] ),
            FILTER (
                ALLSELECTED ( 'Table' ),
                'Table'[Posting_Date] = EARLIER ( 'Table'[Posting_Date] )
                    && 'Table'[Clinic Name] = EARLIER ( 'Table'[Clinic Name] )
            )
        ),
    'Table'[Delivery Charge]
)

 The result is as follow:

vzhengdxumsft_0-1714541317225.png

 

Best Regards

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

Hi Zhengdong,

 

Thanks for the reply. But the outcome not exactly archieve. For multiple orders delivered to same clinic within same day by same driver will consider 1 trip.

So desired deliver fee to clinic F on 2024-02-04 only $5 and not showing $5 twice.

Hi @LilianYip 

 

Please try this:

You can create a measure like this:

MEASURE =
VAR _currentdate =
    MAX ( 'Table'[Posting_Date] )
VAR _currentClinic =
    SELECTEDVALUE ( 'Table'[Clinic Name] )
RETURN
    CALCULATE (
        MAX ( 'Table'[Delivery Charge] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[Clinic Name] = _currentClinic
                && 'Table'[Posting_Date] = _currentdate
        )
    )

The result is as follow:

vzhengdxumsft_1-1714617885302.png

 

Best Regards

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

Hi Zheng Dong,

 

The formula still missed out the driver.

But thanks alot with the help.
Let me try to tweak the formula.

lbendlin
Super User
Super User

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ30TcyMDJR0lHy9Qx2dnH10bUwNjIGcp1zMvMykxUcgUyXosyy1CIw01QpVgdFV7CnrpGJroGxobmpEUKXE9G6zCwQupwRupzw6zI3wWmXoQFObRZGlghtLqiWQbUZYdNmhmSbKzY3YtVlaYzTZ/gsszRFaHPDFozYdFkiRxmmrlgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Posting_Date = _t, Sales_Invoice_No = _t, #"Clinic Name" = _t, #"Driver " = _t, #"Delivery Charge" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Posting_Date", type date}, {"Sales_Invoice_No", type text}, {"Clinic Name", type text}, {"Driver ", type text}, {"Delivery Charge", type number}}),
    #"Replaced Value1" = Table.ReplaceValue(#"Changed Type",each [Delivery Charge],each [Delivery Charge]+Number.Random()/10000,Replacer.ReplaceValue,{"Delivery Charge"}),
    #"Grouped Rows" = Table.Group(#"Replaced Value1", {"Clinic Name", "Driver ", "Posting_Date"}, {{"Desired Delivery Charge", each List.Max([Delivery Charge]), type nullable number}, {"Rows", each _, type table [Posting_Date=nullable date, Sales_Invoice_No=nullable text, Clinic Name=nullable text, #"Driver "=nullable text, Delivery Charge=nullable number]}}),
    #"Expanded Rows" = Table.ExpandTableColumn(#"Grouped Rows", "Rows", {"Sales_Invoice_No", "Delivery Charge"}, {"Sales_Invoice_No", "Delivery Charge"}),
    #"Replaced Value" = Table.ReplaceValue(#"Expanded Rows",each [Desired Delivery Charge],each if [Desired Delivery Charge]=[Delivery Charge] then [Desired Delivery Charge] else null,Replacer.ReplaceValue,{"Desired Delivery Charge"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Replaced Value",{{"Desired Delivery Charge", Int64.Type}, {"Delivery Charge", Int64.Type}})
in
    #"Changed Type1"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Once you examined the code, replace the Source step with your own source.

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors