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.
Hello, I am trying to setup a measure in Power BI that counts the number of a specific bundle that is sold at my company. I have an invoice data table along with a product table and I want to count how many times we sold the combination of "A, B, and C" Product Components on each unique invoice. In the below table the correct answer would be 2 because we have 3 positive occurrences of the bundle and 1 negative occurrence indicating a returned bundle. There are also situations where there may be 2 bundles sold on one invoice, or multiple of one item but you can only classify 1 "bundle" because the other items only have 1 unit each. Please let me know if you need more information to assist with this since I am relatively new to DAX. I greatly appreciate any assistance.
Invoice | Product Component | Units |
111 | A | 1 |
111 | B | 1 |
111 | C | 1 |
111 | D | 1 |
222 | A | 2 |
222 | B | 3 |
222 | C | 2 |
333 | A | 1 |
333 | C | 1 |
333 | X | 1 |
333 | V | 1 |
444 | B | 1 |
444 | A | 1 |
444 | V | 1 |
555 | A | -1 |
555 | B | -1 |
555 | C | -1 |
Solved! Go to Solution.
# Bundles =
SUMX(
VALUES( InvoiceDetails[Invoice] ),
var __invoiceDetails =
CALCULATETABLE(
InvoiceDetails,
ALLEXCEPT( InvoiceDetails, InvoiceDetails[Invoice] )
)
var __onlyABCLines =
FILTER(
__invoiceDetails,
InvoiceDetails[Component] in {"A", "B", "C"}
)
var __ABCLinesAggregated =
addcolumns(
summarize(
__onlyABCLines,
InvoiceDetails[Component]
),
"UnitsSum",
var __component = InvoiceDetails[Component]
var __invWithComponent =
filter(
__onlyABCLines,
InvoiceDetails[Component] = __component
)
return
sumx(
__invWithComponent,
InvoiceDetails[Units]
)
)
var __abcPresent = COUNTROWS( __ABCLinesAggregated ) = 3
var __min = MINX( __ABCLinesAggregated, [UnitsSum] )
var __max = MAXX( __ABCLinesAggregated, [UnitsSum] )
return
if( __abcPresent,
if( __min > 0, __min, __max ),
0
)
)
Please try the above. It should now work....
Best
D
@Anonymous You like criticising people 😀😁
I appreciate the reply D, do you have an example of what you would do? I am pretty new to DAX so not sure how I would edit the above Measure based on your input. Thank you! - Ken
// Columns must always be qualified
// with the table name. Measures
// should never be qualified with
// table name. The exception is
// when you refer to columns added
// to a table via a function.
Measure =
VAR __Table =
FILTER (
'Table',
'Table'[Units] > 0
)
VAR __Table1 =
ADDCOLUMNS(
VALUES( 'Table'[Invoice] ),
"A",
var __currentInv = 'Table'[Invoice]
return
SUMX (
FILTER (
__Table,
and(
'Table'[Invoice] = __currentInv,
'Table'[Product Component] = "A"
)
),
'Table'[Units]
),
"B",
var __currentInv = 'Table'[Invoice]
return
SUMX (
FILTER (
__Table,
and(
'Table'[Invoice] = __currentInv,
'Table'[Product Component] = "B"
)
),
'Table'[Units]
),
"C",
var __currentInv = 'Table'[Invoice]
return
SUMX (
FILTER (
__Table,
and(
'Table'[Invoice] = __currentInv,
'Table'[Product Component] = "C"
)
),
'Table'[Units]
)
)
RETURN
COUNTROWS (
FILTER (
__Table1,
true()
&& [A] > 0
&& [B] > 0
&& [C] > 0
)
)
Hi D, thank you for the response! Similar to Greg's solution when I place this measure in a table with the Invoice numbers I only see Invoices 111 and 222 with the measure flagging 1 bundle per invoice. Ideally the output of the measure would show the below output. Is it possible to adjust the DAX measure to get this output? Thank you! - Ken
Invoice | Bundles (Measure) |
111 | 1 |
222 | 2 |
333 | 0 |
444 | 0 |
555 | -1 |
Total | 2 |
Well, of course, it's similar because it's the same. I just translated what Greg produced into a more digestible and safer version. But the output must be the same.
Has @Anonymous got a better and correct solution? If he has, then let me know. If he has not, then I'll look into this a bit more.
Best
D
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
49 | |
27 | |
20 | |
15 | |
12 |
User | Count |
---|---|
58 | |
50 | |
44 | |
19 | |
18 |