Expression returning wrong rows - what am I doing wrong?

Hi, so I have this expression:

sum(select(Materials[Quantity], and([Material] = [_THISROW].[Material], [Batch]=any(Open_Batches[Batch ID])))) - any(select(Leftovers[Quantity], and([Material] = [_THISROW].[Material], [Batch] = any(Open_Batches[Batch ID]).[PreviousBatch])))

It runs from a behaviour iterating through each material type, and calculates the total used for each material type for the batch by summing the quantities of materials in another table (these rows belong to the batch), and subtracting the amount the batch started with (=the leftovers from the previous batch, also rows in another table belonging to the batch). It mostly works fine, the only problem is that the second statement is returning leftovers belonging to the current batch rather than the previous one, which is what I want.

Open_Batches is a slice. It only ever contains one row.
[PreviousBatch] is a ref to another batch in Batches.
Leftovers.[Batch] is also a reference to a batch (the one it belongs to).

I really canโ€™t see why this isnโ€™t behaving as expected, so any help would be appreciated.

Thanks!

Solved Solved
0 3 194
1 ACCEPTED SOLUTION

Steve
Platinum 4
Platinum 4

This is a problem: any(Open_Batches[Batch ID]).[PreviousBatch]. You cannot dereference an expression.

Try replacing [Batch] = any(Open_Batches[Batch ID]).[PreviousBatch] with this:

IN(
  [Batch],
  SELECT(
    Batches[PreviousBatch],
    IN([Batch ID], Open_Batches[Batch ID])
  )
)

View solution in original post

3 REPLIES 3

Steve
Platinum 4
Platinum 4

This is a problem: any(Open_Batches[Batch ID]).[PreviousBatch]. You cannot dereference an expression.

Try replacing [Batch] = any(Open_Batches[Batch ID]).[PreviousBatch] with this:

IN(
  [Batch],
  SELECT(
    Batches[PreviousBatch],
    IN([Batch ID], Open_Batches[Batch ID])
  )
)

Ah that works as expected, thank you. I didnโ€™t realise you couldnโ€™t dereference an expression - maybe that should throw an exception?

It should, yes. My impressionโ€“based on responses Iโ€™ve gotten to my own similar queries in the pastโ€“is that the library used to parse expressions has limitations that make catching errors like this difficult.

Top Labels in this Space