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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
HPauwelyn
Frequent Visitor

Calculate formula dependent on multiple rows in a table

I've next data:

VoucherID | TimeTypeID | Value 
--------- | ---------- | -----
1 | Arrival | 10:00
1 | Departure | 15:00
1 | Break | 01:00
1 | Travel | 00:30
2 | Arrival | 09:00
2 | Departure | 22:00
2 | Break | 01:00
2 | Travel | 00:45
3 | Arrival | 10:30

Now I'll to calculate the time between the arrival and the departure substracting with the break only when the 3 times are known. The travel can be ignored at the formula. Something like this formula:

Departure - Arrival - Break

I'll get this data:

  • Voucher 1: 04:00
  • Voucher 2: 12:00
  • Voucher 3: null ('cause departure and break are not known)

How could I do that? I know that I must create a measure but I don't know how I could write the code for that.

With the result of that, I'll get a minimal, maximal and average value.

2 ACCEPTED SOLUTIONS
Mariusz
Community Champion
Community Champion

Hi @HPauwelyn 

You can Pivot the data in Query Editor and do the calculation easily like below.
However if for some reason you prefer Measure that can be arranged as well.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXIsKsosS8wBsgwNrAwMlGJ1IOIuqQWJRSWlRakgGVNkGaei1MRsIG1giCwaUpRYlgoyxsDAyhgibIRivIElTLkRmvFGRsgymMYboRlvYgoWNkZ3PcjaWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [VoucherID = _t, TimeTypeID = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Value", type time}, {"VoucherID", Int64.Type}}),
    #"Pivoted Column" = Table.Pivot(#"Changed Type", List.Distinct(#"Changed Type"[TimeTypeID]), "TimeTypeID", "Value", List.Average),
    #"Added Custom" = Table.AddColumn(#"Pivoted Column", "Custom", each Time.From(Number.From([Departure]) - Number.From([Arrival]) - Number.From([Break])), type time)
in
    #"Added Custom"

Regards,
Mariusz

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

Mariusz
Community Champion
Community Champion

Hi @HPauwelyn 

 

DAX Measure below.

 

Measure = 
VAR Departure = CALCULATE(
    SUM(Table1[Value]),
    ALLEXCEPT(Table1, Table1[VoucherID]),
    Table1[TimeTypeID] = "Departure"
)
VAR Arrival = CALCULATE(
    SUM(Table1[Value]),
    ALLEXCEPT(Table1, Table1[VoucherID]),
    Table1[TimeTypeID] = "Arrival"
)
VAR Break = CALCULATE(
    SUM(Table1[Value]),
    ALLEXCEPT(Table1, Table1[VoucherID]),
    Table1[TimeTypeID] = "Break"
)
RETURN IF( Departure > 0 && Break > 0, Departure - Arrival - Break)

 

Regards,
Mariusz

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

3 REPLIES 3
Mariusz
Community Champion
Community Champion

Hi @HPauwelyn 

 

DAX Measure below.

 

Measure = 
VAR Departure = CALCULATE(
    SUM(Table1[Value]),
    ALLEXCEPT(Table1, Table1[VoucherID]),
    Table1[TimeTypeID] = "Departure"
)
VAR Arrival = CALCULATE(
    SUM(Table1[Value]),
    ALLEXCEPT(Table1, Table1[VoucherID]),
    Table1[TimeTypeID] = "Arrival"
)
VAR Break = CALCULATE(
    SUM(Table1[Value]),
    ALLEXCEPT(Table1, Table1[VoucherID]),
    Table1[TimeTypeID] = "Break"
)
RETURN IF( Departure > 0 && Break > 0, Departure - Arrival - Break)

 

Regards,
Mariusz

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

@Mariusz: It's looks good but I only det the average, but it's not a problem to get a minimum and maximum.

Mariusz
Community Champion
Community Champion

Hi @HPauwelyn 

You can Pivot the data in Query Editor and do the calculation easily like below.
However if for some reason you prefer Measure that can be arranged as well.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXIsKsosS8wBsgwNrAwMlGJ1IOIuqQWJRSWlRakgGVNkGaei1MRsIG1giCwaUpRYlgoyxsDAyhgibIRivIElTLkRmvFGRsgymMYboRlvYgoWNkZ3PcjaWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [VoucherID = _t, TimeTypeID = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Value", type time}, {"VoucherID", Int64.Type}}),
    #"Pivoted Column" = Table.Pivot(#"Changed Type", List.Distinct(#"Changed Type"[TimeTypeID]), "TimeTypeID", "Value", List.Average),
    #"Added Custom" = Table.AddColumn(#"Pivoted Column", "Custom", each Time.From(Number.From([Departure]) - Number.From([Arrival]) - Number.From([Break])), type time)
in
    #"Added Custom"

Regards,
Mariusz

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

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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