Find the first value by category - dax - powerbi

I'm trying to find out how to choose first and last date and quantity for each category, here ORDER_LINE_RELEASE_NO.
ORDER_LINE_RELEASE_NO
WANTED_DATE_OLD
WANTED_DATE_NEW
BUY_QTY_DUE_OLD
BUY_QTY_DUE_NEW
49562_1_9
27.01.2022
1
49562_1_9
27.01.2022
27.01.2022
1
2660
50081_1_1
31.01.2022
6
50081_1_1
31.01.2022
31.03.2022
6
6
50081_1_1
31.03.2022
31.03.2022
6
1210
50084_1_1
10.02.2022
1
50084_1_1
10.02.2022
10.03.2022
1
1
50084_1_1
10.03.2022
10.06.2022
1
1
50084_2_1
10.02.2022
60
50084_2_1
10.02.2022
08.04.2022
60
60
52370_1_1
13.05.2022
3000
52370_1_1
13.05.2022
13.05.2022
3000
2000
In this original table I have the same ORDER_LINE_RELEASE_NO in more rows and I would like to "summarize" it like in the second table here:
ORDER_LINE_RELEASE_NO
FIRST_DATE
LAST_DATE
ORIGINAL_QTY
LAST_WANTED_QTY
49562_1_9
27.01.2022
27.01.2022
1
2660
50081_1_1
31.01.2022
31.03.2022
6
1210
50084_1_1
10.02.2022
10.06.2022
1
1
50084_2_1
10.02.2022
08.04.2022
60
60
52370_1_1
13.05.2022
13.05.2022
3000
2000
So basically in the column FIRST_DATE we have the first value from column WANTED_DATE_NEW (for each category), in LAST_DATE the last value from WANTED_DATE_NEW, in ORIGINAL_QTY is the first value from BUY_QTY_DUE_NEW and in LAST_WANTED_QTY we have the last value from BUY_QTY_DUE_NEW.
I tried to use FIRSTNONBLANK and LASTNONBLANK functions, but they only work fot dates, not for all quantity - for example for 52370_1_1 quantity.
My code in creating new table from another in powerBI was:
PURCH_ORD_LINE_UNIQUE =
ADDCOLUMNS (
DISTINCT ( PURCH_ORD_LINE_ARCH[ORDER_LINE_RELEASE_NO] ),
"FIRST_DATE",
CALCULATE (
FIRSTNONBLANK (
PURCH_ORD_LINE_ARCH[WANTED_DATE_NEW],
PURCH_ORD_LINE_ARCH[WANTED_DATE_NEW]
),
ALLEXCEPT ( PURCH_ORD_LINE_ARCH, PURCH_ORD_LINE_ARCH[ORDER_LINE_RELEASE_NO] )
),
"LAST_DATE",
CALCULATE (
LASTNONBLANK (
PURCH_ORD_LINE_ARCH[WANTED_DATE_NEW],
PURCH_ORD_LINE_ARCH[WANTED_DATE_NEW]
),
ALLEXCEPT ( PURCH_ORD_LINE_ARCH, PURCH_ORD_LINE_ARCH[ORDER_LINE_RELEASE_NO] )
),
"ORIGINAL_QTY",
CALCULATE (
FIRSTNONBLANK (
PURCH_ORD_LINE_ARCH[BUY_QTY_DUE_NEW],
PURCH_ORD_LINE_ARCH[BUY_QTY_DUE_NEW]
)
),
"LAST_WANTED_QTY",
CALCULATE (
LASTNONBLANK (
PURCH_ORD_LINE_ARCH[BUY_QTY_DUE_NEW],
PURCH_ORD_LINE_ARCH[BUY_QTY_DUE_NEW]
)
)
)
Sorry if my question is too stupid, I'm quite new to DAX and PowerBI.
Thanks for any answer.
Tomas

here is my solution : (i will share the sample file at the end, so you can easily understand my approach)
First we will add an index column to the table :
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1, Int64.Type)
then, we will add grouped index number column :
Group Ranking =
RANKX (
FILTER (
'Table',
EARLIER ( 'Table'[ORDER_LINE_RELEASE_NO ] ) = 'Table'[ORDER_LINE_RELEASE_NO ]
),
'Table'[Index],
,
ASC,
DENSE
)
finally, we create our table :
Modelling --> New Table
Result Table =
ADDCOLUMNS (
SUMMARIZE ( 'Table', 'Table'[ORDER_LINE_RELEASE_NO ] ),
"FIRST_DATE", CALCULATE ( MIN ( 'Table'[WANTED_DATE_OLD ] ) ),
"LAST_DATE", CALCULATE ( MAX ( 'Table'[WANTED_DATE_new ] ) ),
"ORIGINAL_QTY",
VAR _max =
CALCULATE (
MIN ( 'Table'[Group Ranking] ),
ALLEXCEPT ( 'Table', 'Table'[ORDER_LINE_RELEASE_NO ] ),
NOT ( ISBLANK ( 'Table'[BUY_QTY_DUE_OLD ] ) )
)
RETURN
CALCULATE (
FIRSTNONBLANK ( 'Table'[BUY_QTY_DUE_OLD ], 1 ),
ALLEXCEPT ( 'Table', 'Table'[ORDER_LINE_RELEASE_NO ] ),
'Table'[Group Ranking] = _max
),
"LAST_WANTED_QTY",
VAR _max =
CALCULATE (
MAX ( 'Table'[Group Ranking] ),
ALLEXCEPT ( 'Table', 'Table'[ORDER_LINE_RELEASE_NO ] )
)
RETURN
CALCULATE (
SUM ( 'Table'[BUY_QTY_DUE_NEW] ),
ALLEXCEPT ( 'Table', 'Table'[ORDER_LINE_RELEASE_NO ] ),
'Table'[Group Ranking] = _max
)
)
Here is the sample PBix File for you...

Related

Filter summarizecolumns table within measure using slicer - DAX for Power Pivot

I have a table my_data with the following columns: CATEGORY, SUPPLIER and AMOUNT. I have created measures to calculate the total amount:
Total_Amount := SUM(my_data[AMOUNT])
and to do an ABC classification, I created a ranking:
Ranking:=IF (
ISBLANK ( [Total_Amount] ),
BLANK (),
RANKX (
FILTER (
ALL ( my_data[CATEGORY], my_data[SUPPLIER] ),
my_data[CATEGORY] = MAX ( my_data[CATEGORY] )
),
CALCULATE ( [Total_Amount] )
)
)
a Running Total:
Running_Total:=VAR current_rank = [Ranking]
RETURN
SUMX (
FILTER (
ALL ( my_data[SUPPLIER] ),
[Ranking] <= current_rank
),
[Total_Amount]
)
a Running Total %:
Running_Total(%):=DIVIDE (
[Running_Total],
SUMX ( ALL ( my_data[SUPPLIER] ), [Total_Amount] ),
BLANK ()
)
and the ABC classifier:
ABC_class:=IF (
ISBLANK ( [Total_Amount] ),
BLANK (),
SWITCH (
TRUE (),
[Running_Total(%)] <= [Class_A], "A",
[Running_Total(%)] <= [Class_B], "B",
"C"
)
)
Now, my problem. I have several slicers. Once of them is to choose A, B and/or C from the ABC classification. Note, that in my data there is no column with A, B or C data. The classification is only a measure. So I used a trick to be able to connect the slicer to my Pivot Table and that works fine. PROBLEM: I do not manage to get the right subtotals and grand totals for the following measure:
measure:=VAR Selected_Class =
ALLSELECTED ( ABC_table[Class] )
VAR Supplier_Class =
CALCULATE (
[ABC_class],
ALLEXCEPT (
my_data,
my_data[CATEGORY],
my_data[SUPPLIER],
Period_Table[YEAR]
)
)
RETURN
IF (
HASONEFILTER ( my_data[SUPPLIER] ),
IF (
CONTAINSROW ( Selected_Class, Supplier_Class ),
[Total_Amount],
BLANK ()
),
SUMX (
FILTER (
SUMMARIZECOLUMNS (
StockMvts[SUPPLIER],
"total", [Total_Purchased(EUR)],
"class", [ABC_TotPurchased_byCat&Sup]
),
CONTAINSROW(Selected_Class, [class])
),
[total]
)
)
This last measure doesn't work. It gives an error. The problem is in the SUMX for the subtotals and grand totals. How do I make it to sum only the values of [Total_Amount] for the [SUPPLIER] where the [ABC_class] measure results in one of the values selected in the ABC slicer?
Note, I'm using Power Pivot on Excel.
Thank you!

a measures for comparing two values and counting the result in Power BI

How can I write a measure to count the number of userID for which sum(x1) is equal to count(order_id), in Power BI?
For example, my data table is:
userID
x1
order_id
141
1
719
172
0
616
172
0
189
172
0
2211
172
0
317
1103
1
98
1103
1
213
1103
1
15
2524
0
4902
2524
1
3620
and I use table visual of power bi for this, to explain my mean:
userID
sum(x1)
count(order_id)
141
1
1
172
0
4
1103
3
3
2524
1
2
Note that the userID column is one of the columns in my data table, and calculating sum(x1) and count(order_id) in this sample is by Power BI default features.
The result for this sample should be 2. I need a measure that returns 2.
Measure1 =
VAR _base1 =
SUMMARIZE ( 'Table 1', 'Table 1'[userID] )
VAR _base2 =
ALLEXCEPT ( 'Table 1', 'Table 1'[userID] )
VAR _ct =
ADDCOLUMNS ( _base1, "X", CALCULATE ( COUNT ( 'Table 1'[order_id] ), _base2 ) )
VAR _sum =
ADDCOLUMNS ( _base1, "X", CALCULATE ( SUM ( 'Table 1'[x1] ), _base2 ) )
VAR _nt =
NATURALINNERJOIN ( _sum, _ct )
RETURN
COUNTROWS ( _nt )
or
Measure4 =
VAR _1 =
COUNTX (
VALUES ( 'Table 1'[userID] ),
VAR _base =
ALLEXCEPT ( 'Table 1', 'Table 1'[userID] )
VAR _1 =
CALCULATE ( SUM ( 'Table 1'[x1] ), _base )
VAR _2 =
CALCULATE ( COUNTROWS ( 'Table 1' ), _base )
VAR _3 =
IF ( _1 = _2, 1 )
RETURN
_3
)
RETURN
_1
This should work
count_valid_rows =
VAR sum_x1_table =
SUMMARIZECOLUMNS ( 'table'[userID], 'table', "sumx1", SUM ( 'table'[x1] ) )
VAR count_orderId_table =
SUMMARIZECOLUMNS (
'table'[userID],
'table',
"countOfOrders", COUNT ( 'table'[x1] )
)
RETURN
COUNTROWS (
FILTER (
NATURALINNERJOIN ( sum_x1_table, count_orderId_table ),
[sumx1] = [countOfOrders]
)
)
Docs of the functions used.
NATURALINNERJOIN
SUMMARIZECOLUMNS
Another suggestion:
Count :=
SUMX (
SUMMARIZECOLUMNS (
'Table'[userID] ,
"Sum" , SUM ( 'Table'[x1] ),
"Count" , COUNT ( 'Table'[order_id] )
),
IF ( [Sum] = [Count] , 1 )
)
As you see from the other answers there are heaps of ways to calculate this. I suggest you look over all the suggestions to understand what is going on in each, and then write out your preferred way of dealing with this type of issue after.
Your new measure may looks like this one:
calculate( countrows('YourTabel'), FILTER(ALL('YourTabel'), somestatementIfneeded && var __x1 = [x1] var __x2 = [x2] return __x1 = __x2))
The main part is to use variable PLACEHOLDER;

Count Occurances of value in 2 columns

I have a simple job to do in PowerBi but for some reason I can not get my head around it.
I have projects table - in this table we have "date of start" and "date of end". What I am after is a smaller table with "Year & Quarter" first column and a count of projects started and count of project ended in that "Year & Quarter".
Project Number
Started
Ended
xxxx23
2019-01
2019-03
xxxx24
2019-03
2020-01
xxxx25
2019-03
2020-02
what I am after is something like below:
Year & Quarter
Project Started
Project Ended
2019-01
1
0
2019-03
2
1
2019-04
0
0
When doing the calculations, I am getting wrong counts. Any help is really appreciated!
You can use UNION to create a new table. You can use the original table twice but with different columns.
NewTable =
UNION(
SELECTCOLUMNS( Projects,
"date",Projects[started],
"project",Projects[project],
"Started",1,
"Ended",0
)
,
SELECTCOLUMNS( Projects,
"date",Projects[ended],
"project",Projects[project],
"Started",0,
"Ended",1
)
)
If you have a Calendar Table, you can achieve the ask by doing following two ways
#1
newTable =
VAR _1 =
SUMMARIZE ( 'Calendar', 'Calendar'[Yr & Qt] )
VAR _2 =
ADDCOLUMNS (
ADDCOLUMNS (
_1,
"Project Started",
COUNTX (
FILTER ( Projects, Projects[started] = EARLIER ( [Yr & Qt] ) ),
[started]
)
),
"Project Ended", COUNTX ( FILTER ( Projects, Projects[ended] = EARLIER ( [Yr & Qt] ) ), [ended] )
)
RETURN
_2
#2
Table 2 =
ADDCOLUMNS (
ADDCOLUMNS (
SUMMARIZE ( 'Calendar', 'Calendar'[Yr & Qt] ),
"Project Start Count",
CALCULATE (
CALCULATE (
COUNTX ( Projects, Projects[started] ),
TREATAS ( SUMMARIZE ( 'Calendar', 'Calendar'[Yr & Qt] ), Projects[started] )
)
)
),
"Project End Count",
CALCULATE (
CALCULATE (
COUNTX ( Projects, Projects[ended] ),
TREATAS ( SUMMARIZE ( 'Calendar', 'Calendar'[Yr & Qt] ), Projects[ended] )
)
)
)
results in following
The solution has minimum dependency to a Calendar tbl with a column like Yr & Qt as following

Countrows/calculate/sum rows in DAX

I am fairly new with Power BI and DAX and I'm stuck. I'll try to explain the current situation and what I want to become my output. I've tried a lot of meaures with distinctcount, calculate, you name it, I did it. But can't find the right solution.
We've got 4 columns: Date, Employee_ID, Sick, %FTE. Every row records if an employee was sick on that date. Blank is not sick and Y = sick.
I would like to create a measure where it counts the %FTE just once when an employee is sick in a particular week, month or year.
So the output of January should be 2,13 (0,8 + 0,33 + 1) and in February 1,8 (0,8 + 1).
enter image description here
You would need two additional columns in the dataset as following
Once you have that, you can use the following measures to reach the goal
Measure8 =
VAR _1 =
IF (
MAX ( 'fact'[sick] ) <> BLANK (),
RANKX (
FILTER (
ALL ( 'fact' ),
'fact'[emp_id] = MAX ( 'fact'[emp_id] )
&& 'fact'[Year] = MAX ( 'fact'[Year] )
&& 'fact'[Month] = MAX ( 'fact'[Month] )
&& 'fact'[sick] = "Y"
),
CALCULATE ( MAX ( 'fact'[date] ) ),
,
ASC,
DENSE
)
)
VAR _2 =
IF ( _1 = 1, IF ( MAX ( 'fact'[sick] ) = "y", MAX ( 'fact'[%FTE] ) ) )
RETURN
_2
Measure9 =
IF (
HASONEVALUE ( 'date'[date] ),
[Measure8],
VAR _1 =
MAXX (
GROUPBY (
ADDCOLUMNS ( 'fact', "val", [Measure8] ),
[Year],
[Month],
"total", SUMX ( CURRENTGROUP (), [val] )
),
[total]
)
VAR _2 =
MAXX (
GROUPBY (
ADDCOLUMNS ( 'fact', "val", [Measure8] ),
[Year],
"total", SUMX ( CURRENTGROUP (), [val] )
),
[total]
)
VAR _3 =
IF ( ISINSCOPE ( 'fact'[Year] ), _2, _1 )
RETURN
_3
)
Also, for any future posts please provide the sample data and expected output as markdown tables How To

Power BI Dax Group By Measure - Display Dates Before Today

I am trying to create a measure that will show booked revenue over time, grouped by a few categories. I also want to show as 0 within line charts when the data is BLANK(), so I have it in an IF statement. Everything works just fine except for whatever reason I cannot get it to only show dates prior to today.
Here is the current Measure:
M_BookedRevenue =
VAR CurrentAccountingYear =
CALCULATE ( MIN ( 'Date'[AccountingYear] ), 'Date'[Date] = TODAY () )
VAR CurrentDate =
CALCULATE ( MIN ( 'Date'[SortDate] ), 'Date'[Date] = TODAY() )
RETURN
CALCULATE (
IF (
ISBLANK (
SUMX (
FILTER (
GROUPBY (
RevenueChannel,
RevenueChannel[ChannelBucket],
RevenueChannel[Channel],
'Date'[Date],
'Date'[SortDate],
'Date'[AccountingWeek],
'Date'[WeekNumber],
'Date'[AccountingMonthEnglishAbbrYear],
'Date'[AccountingMonth],
'Date'[AccountingYear],
"Revenue", SUMX ( CURRENTGROUP (), RevenueChannel[TransactionRevenue] )
),
'Date'[AccountingYear] >= CurrentAccountingYear
&& 'Date'[SortDate] < CurrentDate
),
[Revenue]
)
),
0,
SUMX (
FILTER (
GROUPBY (
RevenueChannel,
RevenueChannel[ChannelBucket],
RevenueChannel[Channel],
'Date'[Date],
'Date'[SortDate],
'Date'[AccountingWeek],
'Date'[WeekNumber],
'Date'[AccountingMonthEnglishAbbrYear],
'Date'[AccountingMonth],
'Date'[AccountingYear],
"Revenue", SUMX ( CURRENTGROUP (), RevenueChannel[TransactionRevenue] )
),
'Date'[AccountingYear] >= CurrentAccountingYear
&& 'Date'[SortDate] < CurrentDate
),
[Revenue]
)
)
)