|
| 1 | +/// Return the value computed in the filtered ABC classes |
| 2 | +/// Implement Dynamic ABC Classification pattern from DAX Patterns |
| 3 | +/// Article: https://www.daxpatterns.com/abc-classification/ |
| 4 | +/// You must have a table with the definition of |
| 5 | +/// the ABC classes (abcClassTable) with at least two columns, |
| 6 | +/// the lower and the upper boundary of each class |
| 7 | +/// The first argument is the expression to evaluate for each |
| 8 | +/// ite in itemTable, which cardinality is expressed by itemKeyColumn |
| 9 | +/// The transactionTable is the table that defines the presence of |
| 10 | +/// transactions, typically the table that has the column(s) |
| 11 | +/// aggregated in valueExpr |
| 12 | +function 'DaxPatterns.AbcClassification.ComputeInAbcClass' = ``` |
| 13 | + ( |
| 14 | + valueExpr : ANYREF EXPR, |
| 15 | + transactionsTable : ANYREF EXPR, |
| 16 | + itemTable : ANYREF EXPR, |
| 17 | + itemKeyColumn : ANYREF EXPR, |
| 18 | + abcClassTable : ANYREF EXPR, |
| 19 | + abcLowerBoundaryColumn : ANYREF EXPR, |
| 20 | + abcUpperBoundaryColumn : ANYREF EXPR |
| 21 | + ) => |
| 22 | + VAR ProductsInClass = |
| 23 | + DaxPatterns.AbcClassification.ItemsInClass( |
| 24 | + valueExpr, |
| 25 | + TransactionsTable, |
| 26 | + ItemTable, |
| 27 | + ItemKeyColumn, |
| 28 | + abcClassTable, |
| 29 | + abcLowerBoundaryColumn, |
| 30 | + abcUpperBoundaryColumn |
| 31 | + ) |
| 32 | + VAR Result = |
| 33 | + CALCULATE ( |
| 34 | + valueExpr, |
| 35 | + KEEPFILTERS ( ProductsInClass ) |
| 36 | + ) |
| 37 | + RETURN Result |
| 38 | + ``` |
| 39 | + annotation DAXLIB_PackageId = DaxPatterns.AbcClassification |
| 40 | + |
| 41 | + annotation DAXLIB_PackageVersion = 0.1.2 |
| 42 | + |
| 43 | +/// Return the items filtered by ABC classes |
| 44 | +/// Implement Dynamic ABC Classification pattern from DAX Patterns |
| 45 | +/// Article: https://www.daxpatterns.com/abc-classification/ |
| 46 | +/// You must have a table with the definition of |
| 47 | +/// the ABC classes (abcClassTable) with at least two columns, |
| 48 | +/// the lower and the upper boundary of each class |
| 49 | +/// The first argument is the expression to evaluate for each |
| 50 | +/// ite in itemTable, which cardinality is expressed by itemKeyColumn |
| 51 | +/// The transactionTable is the table that defines the presence of |
| 52 | +/// transactions, typically the table that has the column(s) |
| 53 | +/// aggregated in valueExpr |
| 54 | +function 'DaxPatterns.AbcClassification.ItemsInClass' = ``` |
| 55 | + ( |
| 56 | + valueExpr : ANYREF EXPR, |
| 57 | + transactionsTable : ANYREF EXPR, |
| 58 | + itemTable : ANYREF EXPR, |
| 59 | + itemKeyColumn : ANYREF EXPR, |
| 60 | + abcClassTable : ANYREF EXPR, |
| 61 | + abcLowerBoundaryColumn : ANYREF EXPR, |
| 62 | + abcUpperBoundaryColumn : ANYREF EXPR |
| 63 | + ) => |
| 64 | + VAR TransactionsByItem = |
| 65 | + CALCULATETABLE ( |
| 66 | + ADDCOLUMNS ( |
| 67 | + SUMMARIZE ( transactionsTable, itemKeyColumn ), |
| 68 | + "@Value", CALCULATE ( valueExpr ) |
| 69 | + ), |
| 70 | + ALLSELECTED ( itemTable ) |
| 71 | + ) |
| 72 | + VAR AllItemsValue = |
| 73 | + SUMX ( |
| 74 | + transactionsByItem, |
| 75 | + [@Value] |
| 76 | + ) |
| 77 | + VAR CumulatedPctByItemTable = |
| 78 | + ADDCOLUMNS ( |
| 79 | + transactionsByItem, |
| 80 | + "@CumulatedPct", |
| 81 | + VAR CumulatedValue = |
| 82 | + SUMX ( |
| 83 | + WINDOW ( |
| 84 | + 1, ABS, |
| 85 | + 0, REL, |
| 86 | + transactionsByItem, |
| 87 | + ORDERBY ( [@Value], DESC ) |
| 88 | + ), |
| 89 | + [@Value] |
| 90 | + ) |
| 91 | + VAR Perc = |
| 92 | + DIVIDE ( |
| 93 | + CumulatedValue, |
| 94 | + AllItemsValue |
| 95 | + ) |
| 96 | + RETURN |
| 97 | + MIN ( Perc, 1 ) -- Avoid >100% in case of rounding issues |
| 98 | + ) |
| 99 | + |
| 100 | + VAR Result = |
| 101 | + FILTER ( |
| 102 | + CROSSJOIN ( |
| 103 | + CumulatedPctByItemTable, |
| 104 | + abcClassTable |
| 105 | + ), |
| 106 | + AND ( |
| 107 | + [@CumulatedPct] > abcLowerBoundaryColumn, |
| 108 | + [@CumulatedPct] <= abcUpperBoundaryColumn |
| 109 | + ) |
| 110 | + ) |
| 111 | + RETURN Result |
| 112 | + ``` |
| 113 | + annotation DAXLIB_PackageId = DaxPatterns.AbcClassification |
| 114 | + |
| 115 | + annotation DAXLIB_PackageVersion = 0.1.2 |
0 commit comments