DAX how to Ignore certain slicers in measure? - powerbi

I have a table like below:
BU Value Date Measure Agg_Lvl_1 Agg_Lvl_2 Agg_Lvl_3
AA 10 01/01/2021 Sale Firm COO A
AB 20 01/04/2021 Sale Firm Non-COO A
AC 32 01/05/2021 Sale Firm COO A
BA 32 01/01/2021 Sale Firm Non-COO B
BB 43 01/04/2021 Sale Firm Non-COO B
BC 19 01/08/2021 Sale Firm Non-COO B
CA 11 01/11/2021 Sale Firm Non-COO C
CB 16 01/12/2021 Sale Firm Non-COO C
CC 18 01/13/2021 Sale Firm COO C
D 18 01/01/2021 Sale Ext Non-CIO D
AA 10 01/01/2021 non-Sale Ext Non-CIO A
AB 20 01/04/2021 non-Sale Firm Non-CIO A
I need to calculate each BU's contribution for Firm Sale by period:
contribution = Sum(Table(Value) where Measure ='Sale' & BU ='slicer select') /
Sum(Table(Value) where Measure ='Sale' & BU ='Firm'
also this "contribution" measure should correspond to date slicer
I have tried different DAX method all i got was contribution of 1 (i think the slicer/filter isn't set up right). Anyone please help?
E.g. AA contribution between 1/1/2021 - 1/4/2021 = (10+20)/ (10+20+32+18) = 12.5%

You can try the function ALLEXCEPT as shown below-
contribution =
CALCULATE(
SUM(Table_name[value]),
FILTER(
ALLEXCEPT(Table_name,Table_name[Date],Table_name[BU]),
Table_name[measure] = "Sale"
)
)
/
CALCULATE(
SUM(Table_name[value]),
FILTER(
ALLEXCEPT(Table_name,Table_name[Date]),
Table_name[measure] = "Sale"
&& Table_name[BU] = "Firm"
)
)

Related

Calculate the average excluding some values

I have a table (TABLE1) similar to this:
DATE
VALUE
01/01/2022
4
01/01/2022
3
01/01/2022
5
01/01/2022
8
02/01/2022
9
02/01/2022
8
02/01/2022
7
02/01/2022
3
I would like to calculate for each day the average value excluding the values that are less than the general average.
For example, for the 01/01/2022 the average is (4+3+5+8)/4 = 5 and the value that I want to calculate is the average excluding the values undder than this average (5+8)/2 = 6,5
Hope you can help me with a measure to calculate this.
Thanks!!
Test this measure:
AverageValue =
VAR AllAverage = CALCULATE(AVERAGE(TestTable[VALUE]),ALLEXCEPT(TestTable,TestTable[DATE]))
VAR TblSummary = ADDCOLUMNS(
VALUES(TestTable[DATE]),
"AvgValue",CALCULATE(AVERAGE(TestTable[VALUE]),TestTable[VALUE]>=AllAverage)
)
RETURN
AVERAGEX(TblSummary,[AvgValue])

DAX - Rankx by multiple Categories Issue

I have 4 Categories (GP, ID, Age, Date). I would would like to create calculated column and group by GP, ID, and Age and Rank/ count by Date to see how many months each member has in past 24 month.
My Code works until I have members who cancelled their membership for a few months and then resumed after. I need to restart from the first month after skip. for example :
GP ID AGE DATE RKING Desired RANK
1 220 35-44 202206 12 6
1 220 35-44 202205 12 5
1 220 35-44 202204 12 4
1 220 35-44 202203 12 3
1 220 35-44 202202 12 2
1 220 35-44 202201 12 1
1 220 35-44 202012 24 24
1 220 35-44 202011 23 23
1 220 35-44 202010 22 22
1 220 35-44 202009 21 21
1 220 35-44 202008 20 20
1 220 35-44 202007 19 19
1 220 35-44 202006 18 18
1 220 35-44 202005 17 17
1 220 35-44 202004 16 16
… … … … … …
1 220 35-44 201901 1 1
This is what I have tried but doesn't work for dates skipping.
RKING Column=
RANKX (
CALCULATETABLE (
VALUES ('tbl'[Date] ),
ALLEXCEPT ( 'tblW', 'tbl'[GP], 'tbl'[ID] ),
'tbl'[AGE] = 'tbl'[AGE],
'tbl'[date] >= start_date && 'tbl'[date] <= end_date // date slicer
),
[Date] ,
,ASC
)
Looking through the code you were trying to make a measure for a visual (For a calcCol the measure is added as well). And as I got a point, you want to show a sum of consequtive months in a matrix for each date in accordance to ID/GP/AGE/DATE I see a following way.
As you know, calculations performs for each row in a matrix and filter the data model according to data presented in matrix rows and columns (slicers as well). So, my idea is -
Get date from matrixRow and use it as max date for the table.
Then use a FILTER(). FILTER() is an iterative function, so it goes throw each row and checks filtering condition - if true row remains if false - not.
I use following filtring conditions:
Get dateInMatrix-dateInACurrentTableRow (for example: 202203-202201= 2 months)
Then check how many rows in the table with min=202201 and max<202203
if there are less rows then date difference then it FALSE() and the row is out of table.
3) The last step is counting of rows it a filtered table.
A measure for matrix:
Ranking =
VAR matrixDate=MAX('table'[DATE])
VAR filteredTable =
FILTER(
ALL('table')
,DATEDIFF(
DATE(LEFT([DATE],4),RIGHT([DATE],2),1)
,DATE(LEFT(matrixDate,4),RIGHT(matrixDate,2),1)
,MONTH
)
=
VAR dateInRow=[DATE]
RETURN
CALCULATE(
COUNTROWS('table')
,'table'[DATE]>=dateInRow
,'table'[DATE]<matrixDate
)
)
RETURN
COUNTROWS(filteredTable)
[![enter image description here][1]][1]
A measure for calcColl:
RankColl =
VAR currentDate=[Start_Date]
Var MyFilt={('Table'[AGE],'Table'[ID],'Table'[GROUP])}
VAR withColl =
ADDCOLUMNS(
CALCULATETABLE(
'table'
,ALL('Table')
,TREATAS(MyFilt,'Table'[AGE],'Table'[ID],'Table'[GROUP])
)
,"dateDiff",
DATEDIFF(
[Start_Date]
,currentDate
,MONTH
)
,"RowsInTable",
VAR dateInRow=[Start_Date]
Var startDate=IF(dateInRow<currentDate,dateInRow,currentDate)
VAR endDay =IF(dateInRow>currentDate,dateInRow,currentDate)
VAR myDates = GENERATESERIES(startDate,endDay,1)
RETURN
COUNTROWS(
CALCULATETABLE(
'Table'
,ALL('Table')
,TREATAS(MyFilt,'Table'[AGE],'Table'[ID],'Table'[GROUP])
,TREATAS(myDates,'Table'[Start_Date])
)
)
)
VAR filtered =
FILTER(
withColl
,[dateDiff]=[RowsInTable]-1 -- for ex.:
-- dateDiff=01/01/2022-01/01/2022=0,
-- but it will be 1 row in the table for 01/01/2022
)
RETURN
CountRows( filtered)

How to count the rows of a filtered table in power bi in a calculated column

I want to numerate the occurances of a specific column in my table. The best way I have thought to do this is to count the rows of a filtered table. So, my WorkOrders table looks like this:
WO# Date CompCode Serial#
001 1/1/2021 100 A
001 1/1/2021 101 A
002 1/2/2021 100 B
003 2/1/2021 100 A
004 2/2/2021 100 B
005 2/15/2021 101 A
006 3/1/2021 102 A
006 3/1/2021 100 A
I want to create a new column that numerates the occurance on the CompCode by Serial#. There is no gaurantee that the data is sorted by date. So, I tried to count the rows of a filtered table using this formula:
COMP_OCCURANCE =
CALCULATE(
COUNTROWS(WorkOrders),
Serial# = Serial#,
Date <= Date,
CompCode = CompCode
)
I assumed that would work but it does not. The desired result would look like this:
WO# Date CompCode Serial# COMP_OCCURANCE
001 1/1/2021 100 A 1
001 1/1/2021 101 A 1
002 1/2/2021 100 B 1
003 2/1/2021 100 A 2
004 2/2/2021 100 B 2
005 2/15/2021 101 A 2
006 3/1/2021 102 A 1
006 3/1/2021 100 A 3
Thanks in advance for the help.
Try this:
COMP_OCCURANCE =
VAR CurrentSerial = WorkOrders[Serial #]
VAR CurrentDate = WorkOrders[Date]
VAR CurrentCode = WorkOrders[CompCode]
VAR Result =
CALCULATE (
COUNTROWS ( WorkOrders ),
WorkOrders[Serial #] = CurrentSerial,
WorkOrders[CompCode] = CurrentCode,
WorkOrders[Date] <= CurrentDate,
REMOVEFILTERS ( WorkOrders )
)
RETURN
Result
Screenshot - https://ibb.co/Bj5xrFS
For this type of calculation, you must need to rely on a column for sorting/ordering your data. As you cannot use Date in this case, I think you can go for column "WO#" this case. If this is acceptable, you can try this below measure for your expected output-
COMP_OCCURANCE =
CALCULATE(
COUNT(your_table_name[WO#]),
FILTER(
ALL(your_table_name),
your_table_name[WO#] <= MIN(your_table_name[WO#])
&& your_table_name[CompCode] = MIN(your_table_name[CompCode])
&& your_table_name[Serial#] = MIN(your_table_name[Serial#])
)
)
Output-

DAX formula for sales sum for each day of previous n days

Given a table FACT in Power BI with three columns Date, Category and Sales I am looking for a DAX function that for each day returns the sum of the sales of its previous n days.
Lets assume n = 2, that means for day 01/04/2020 my measure should return the sum of the sales of the days 01/02/2020 and 01/03/2020.
Here is a small example:
Date Category Sales
01/01/2020 A 1
01/01/2020 B 3
01/02/2020 B 2
01/03/2020 B 1
01/04/2020 A 0
01/05/2020 B 10
01/06/2020 B 7
What I want is
Date MyMeasure
01/01/2020 0
01/02/2020 4
01/03/2020 6
01/04/2020 3
01/05/2020 1
01/06/2020 10
I later would then like to use the Category as a filter and in my case n is 365.
I tried the following
MyMeasure =
VAR FROM_DATE =
DATEADD ( FACT[DATE], 0, DAY )
VAR SALES_365 =
CALCULATE (
SUM ( FACT[SALES] ),
DATESINPERIOD ( FACT[DATE], FROM_DATE, 365, DAY )
)
RETURN
SALES_365
but got an error that a table of multiple values was supplied where a single value was expected.
The error was in the variable FROM_DATE, in which DATEADDD returned a column of dates and not a single value.
That said you don't really need a variable for that, you can just use a simple subtraction and DATESBETWEEN(DateCol, DateFrom,DateTo)
MyMeasure =
CALCULATE (
SUM ( FACT[SALES] ),
DATESBETWEEN('FACT'[DATE],MAX(FACT[DATE])-2,MAX(FACT[DATE])-1)
)

How to calculate yearly average per product?

I am new to Power BI and the language DAX. Therefore, I find it hard to do simple yearly average across different products.
I have tried a different combination of the functions AVERAGEX and CALCULATE without any luck so far.
My table:
Date | Product | Value
2014-05-06 Cheese 10
2014-08-11 Cheese 12
2015-04-11 Cheese 9
2014-01-22 Milk 4
2014-12-24 Milk 8
The output I try to create:
Date | Product | Value | Yearly_AVG
2014-05-06 Cheese 10 11
2014-08-11 Cheese 12 11
2015-04-11 Cheese 9 9
2014-01-22 Milk 4 6
2014-12-24 Milk 8 6
Lastly, I do not have a calendar table in the dataset.
For a measure, you can write something like this:
YearAvg =
VAR CurrProduct = VALUES ( Sales[Product] )
VAR CurrYear = YEAR ( MAX ( Sales[Date] ) )
RETURN
CALCULATE (
AVERAGE ( Sales[Value] ),
ALLSELECTED ( Sales ),
Sales[Product] IN CurrProduct,
YEAR ( Sales[Date] ) = CurrYear
)