Search the second max value by multiple conditions in power BI - powerbi

I'm new at power BI and i've a model with table that call DIVUCHIM_MONE.
I want to get the last [Id] value for the [work_order].
The same [work_order] can be appear several time, the [Id] not.
For exmple:
Id work_order
1 215353
2 215325
3 215325
4 215353
5 215221
6 215231
7 215221
8 215325
9 215353
10 215231
I expected to get:
Id work_order Id_Before
1 215353 0
2 215325 0
3 215325 2
4 215353 1
5 215221 0
6 215231 0
7 215221 5
8 215325 3
9 215353 4
10 215231 6

Please try the following measure
_id =
VAR _currentlyVisibleID =
MAX ( 'Fact'[Id] )
VAR _immediatelyPrecedingByWO =
CALCULATE (
CALCULATE ( MAX ( 'Fact'[Id] ), 'Fact'[Id] < _currentlyVisibleID ),
ALLEXCEPT ( 'Fact', 'Fact'[work_order] )
)
RETURN
_immediatelyPrecedingByWO+0
Edit
Calculated Column
Column =
VAR _currentlyVisibleID =
CALCULATE(MAX ( 'Fact'[Id] ))
VAR _immediatelyPrecedingByWO =
CALCULATE (
CALCULATE ( MAX ( 'Fact'[Id] ), 'Fact'[Id] < _currentlyVisibleID ),
ALLEXCEPT ( 'Fact', 'Fact'[work_order] )
)
RETURN
_immediatelyPrecedingByWO+0

Related

Powerbi Top N and others for Pie Chart

I have the following data:
Country Sales
1 20
1 30
1 10
2 25
2 80
3 200
3 4
4 20
5 30
I want to have top 2 country sales summed up and the rest
Country Sales
2 105
3 204
Others 110
I want to display the summed up data in a pie chart.
Rank Sales =
RANKX(ALL(Table[Country]),CALCULATE(sum(Table[Sales])))
What should I do ahead? The if condition is giving an error:
Rank Display =
IF('DAX Measures'[Rank Sales]>2,"Other",'Table'[country])
Please create a new table, and paste this code:
CategorizationByRank =
VAR Summary_01 =
ADDCOLUMNS (
VALUES ( 'Table'[Country] ),
"Total", CALCULATE ( SUM ( 'Table'[Sales] ) )
)
VAR Summary_02 =
ADDCOLUMNS ( Summary_01, "Ranking", RANKX ( Summary_01, [Total] ) )
VAR TablesRankLess_And_Equal_To_2 =
FILTER ( Summary_02, [Ranking] <= 2 )
VAR TablesRankGreater_Than_2 =
ROW (
"Country", "Others",
"Total", SUMX ( FILTER ( Summary_02, [Ranking] > 2 ), [Total] ),
"Ranking", 3
)
VAR UnionAllRecords =
UNION ( TablesRankGreater_Than_2, TablesRankLess_And_Equal_To_2 )
RETURN
UnionAllRecords
And It produces this result:
Then you can put the fields directly into a pie chart, see the picture:

PowerBI - DAX (Find previous time in filtered area and return value from other column)

I am struggling with a task in DAX.
To achieve (yellow column):
Find previous datetime value in filtered scope and return value from other column. I would like to have a ID (MOVE_ID), instead of time of previous move, which is working fine, as below.
Already achieved (green column - working correctly):
I was able to return the value itself (MAX function as below). I was trying to change from yesterday to meet my requirements, without success. I assume it is quite trivial, but skill is missing :)
CALCULATE(
MAX(TABLE1[Move_TIME]),
FILTER(
ALL(TABLE1),
TABLE1[Move_EQ] = EARLIER(TABLE1[Move_EQ])
&& TABLE1[Move_EQ] <> BLANK()
&& [TABLE1Move_TIME] < EARLIER(TABLE1[Move_TIME])
)
)
Move_ID
Move_EQ
Move_TIME
Move_TIME_OF_PREVIOUS_MOVE
Move_ID_OF_PREVIOUS_MOVE
1
EQ1
2021-12-13 11:02:14
null
2
EQ1
2021-12-13 11:03:01
2021-12-13 11:02:14
1
3
EQ2
2021-12-13 11:15:33
null
4
EQ1
2021-12-13 13:12:00
2021-12-13 11:03:01
2
5
EQ2
2021-12-13 14:00:00
2021-12-13 11:15:33
3
6
EQ2
2021-12-13 14:01:00
2021-12-13 14:00:00
5
7
EQ1
2021-12-13 14:32:11
2021-12-13 13:12:00
4
8
EQ1
2021-12-13 14:44:54
2021-12-13 14:32:11
7
9
EQ1
2021-12-13 14:45:01
2021-12-13 14:44:54
8
10
EQ1
2021-12-13 15:01:09
2021-12-13 14:45:01
9
Thank You in advance!
You can write a measure like this
Measure =
VAR _current =
MAX ( 'transaction'[Move_Time] )
VAR _eq =
MAX ( 'transaction'[Move_EQ] )
VAR _immediatelyPrecedingDate =
CALCULATE (
MAX ( 'transaction'[Move_Time] ),
FILTER ( ALL ( 'transaction' ), 'transaction'[Move_Time] < _current ),
'transaction'[Move_EQ] = _eq
)
VAR _id =
CALCULATE (
MAX ( 'transaction'[Move_ID] ),
FILTER (
ALL ( 'transaction' ),
'transaction'[Move_Time] = _immediatelyPrecedingDate
&& 'transaction'[Move_EQ] = _eq
)
)
RETURN
_id
or if you prefer a calculated column, you can take advantage of already created column to create the new column
Move_ID_OF_PREVIOUS_MOVE =
CALCULATE (
MAX ( 'transaction'[Move_ID] ),
FILTER (
ALL ( 'transaction' ),
'transaction'[Move_EQ] = EARLIER ( 'transaction'[Move_EQ] )
&& 'transaction'[Move_EQ] <> BLANK ()
&& 'transaction'[Move_Time] = EARLIER ( 'transaction'[Move_TIME_OF_PREVIOUS_MOVE] )
)
)

Compare 2 rows in same table of PowerBI

I am trying to create a column to see if an ID equals another ID with the same month. It should show "NO" but if an ID equals another ID with a different month. It should show "YES". Other show "NO". see as below
ID Month Duplicate
1 4 No
1 4 No
2 5 No
2 6 Yes
2 7 Yes
3 8 No
4 6 No
4 6 No
4 7 Yes
4 8 Yes
5 6 No
5 6 No
5 6 No
My code like this
Duplicate =
IF (
COUNTROWS ( FILTER ( Data, Data[Policy No] = EARLIER( Data[Policy No]) ) )
> 1
&& COUNTROWS ( FILTER ( Data, Data[Month] < EARLIER(Data[Month]) ) ),
"YES",
"NO")
but it is not correct because when I select the first month as month 4 it shows no but when I select month 5 it shows yes. Like as below
ID month Duplicate
1 4 No
1 4 No
2 5 Yes
2 5 Yes
Please help me resolve this
Thank you
Not sure I understand the logic you want to implement but i THINK this will do what you're looking for (providing I did understand...).
I prefer to use variables rather than the EARLIER function. IMHO it makes the code easier to understand and more readable, especially for others.
This is a calculated column in your 'Data' table
Check =
var _id = [ID]
var _month = [Month]
var _check_both =
CALCULATE(
COUNTROWS('Data'),
FILTER(
ALL('Data'),
'Data'[ID] = _id && 'Data'[Month] = _month
)
)
var _check_id =
CALCULATE(
COUNTROWS('Data'),
FILTER(
ALL('Data'),
'Data'[ID] = _id
)
)
return
SWITCH(
TRUE(),
_check_both > 1, "No",
_check_id > 1, "Yes",
"NO"
)

DAX/M count for consecutive months for every ID in PowerBI

I need help finding a way to count the number of consecutive months:
For every "ID" (Text Column)
Having the column "Status" either "Missing" or "On hold"
Here is what my table looks like (the last two columns are the output I would like to see):
ID
Year
Month
Date (01-Month-Year)
Status
Consecutive Months "Missing"
Consecutive Months "On Hold"
ID40
2019
6
01/06/2019
Missing
0
-
ID40
2019
7
01/07/2019
Missing
2
-
ID40
2019
8
01/08/2019
Missing
3
-
ID40
2019
11
01/11/2019
Missing
0
-
ID40
2019
12
01/12/2019
Missing
2
-
ID40
2020
9
01/09/2020
Missing
0
-
ID499
2019
1
01/01/2019
On Hold
-
0
ID499
2019
2
01/02/2019
On Hold
-
2
ID499
2019
3
01/03/2019
On Hold
-
3
ID499
2020
9
01/09/2020
On Hold
-
0
ID499
2020
10
01/10/2020
On Hold
-
2
ID499
2020
8
01/08/2020
Missing
0
-
ID499
2020
9
01/09/2020
Missing
2
-
ID499
2020
10
01/10/2020
Missing
3
-
ID499
2020
11
01/11/2020
Missing
4
-
ID499
2020
12
01/12/2020
Missing
5
-
Is there any way to do this besides with merged nested queries in "M"? Unfortunately I have already tried this, but PowerBI has trouble processing the data.
Thanks everyone in advance!
A possible solution is to build a table of the Years and Months since the first Year in the table and remove from it the Year Months that appear in the table. Than compute the difference in months between the current Year Month and the last non existent Year Month.
This is a possible calculated column that uses the EXCEPT function to build the table with the missing Year Months. To ease the computation of the difference in months across the years, it also prepares a YearMonthNumber value with the progressive month number from the first year month
Since in the example table there are separate columns for the Status, I added an IF to split the columns accordingly
I also added a check to return 0 instead of 1, to match the example data
Consecutive Months Missing =
IF (
T[Status] = "Missing",
VAR FirstYear =
MIN ( T[Year] )
VAR CurrentYear = T[Year]
VAR CurrentMonth = T[Month]
VAR CurrentYearMonthNumber = ( CurrentYear - FirstYear ) * 12 + CurrentMonth
VAR YearMonths =
GENERATE (
SELECTCOLUMNS ( GENERATESERIES ( FirstYear, CurrentYear ), "Year", [Value] ),
SELECTCOLUMNS (
GENERATESERIES ( 1, 12, 1 ),
"Month", [Value],
"YearMonthNumber",
( [Year] - FirstYear ) * 12 + [Value]
)
)
VAR CurrentIDAndStatusYearMonths =
CALCULATETABLE (
ADDCOLUMNS (
SUMMARIZE ( T, T[Year], T[Month] ),
"YearMonthNumber",
( T[Year] - FirstYear ) * 12 + T[Month]
),
ALLEXCEPT ( T, T[ID], T[Status] )
)
VAR MissingYearMonths =
EXCEPT ( YearMonths, CurrentIDAndStatusYearMonths )
VAR FirstMissingYearMonthNumber =
MAXX (
FILTER ( MissingYearMonths, [YearMonthNumber] < CurrentYearMonthNumber ),
[YearMonthNumber]
)
VAR Result = CurrentYearMonthNumber - FirstMissingYearMonthNumber
RETURN
IF ( Result = 1, 0, Result )
)
and
Consecutive Months On Hold =
IF (
T[Status] = "On Hold",
VAR FirstYear =
MIN ( T[Year] )
VAR CurrentYear = T[Year]
VAR CurrentMonth = T[Month]
VAR CurrentYearMonthNumber = ( CurrentYear - FirstYear ) * 12 + CurrentMonth
VAR YearMonths =
GENERATE (
SELECTCOLUMNS ( GENERATESERIES ( FirstYear, CurrentYear ), "Year", [Value] ),
SELECTCOLUMNS (
GENERATESERIES ( 1, 12, 1 ),
"Month", [Value],
"YearMonthNumber",
( [Year] - FirstYear ) * 12 + [Value]
)
)
VAR CurrentIDAndStatusYearMonths =
CALCULATETABLE (
ADDCOLUMNS (
SUMMARIZE ( T, T[Year], T[Month] ),
"YearMonthNumber",
( T[Year] - FirstYear ) * 12 + T[Month]
),
ALLEXCEPT ( T, T[ID], T[Status] )
)
VAR MissingYearMonths =
EXCEPT ( YearMonths, CurrentIDAndStatusYearMonths )
VAR FirstMissingYearMonthNumber =
MAXX (
FILTER ( MissingYearMonths, [YearMonthNumber] < CurrentYearMonthNumber ),
[YearMonthNumber]
)
VAR Result = CurrentYearMonthNumber - FirstMissingYearMonthNumber
RETURN
IF ( Result = 1, 0, Result )
)
this is the resulting table put in a table visual

How to add 2 columns in Power BI measures using filters on min date?

I have a table like this :
Id numbers_old numbers_new date
1 5 0 2019-02-13
1 8 3 2019-02-14
2 2 0 2019-02-13
2 12 10 2019-02-14
2 15 5 2019-02-15
I want a measure which will calculate volume_total = (number_old where date=min(date)) + (numbers_new where date != min(date))
How can I achieve it with a Power BI measure?
You can write the measure:
volume_total =
VAR DateMin =
MIN ( Table1[date] )
VAR SumOld =
CALCULATE (
SUM ( Table1[numbers_old] ),
Table1[date] = DateMin
)
VAR SumNew =
CALCULATE (
SUM ( Table1[numbers_new] ),
Table1[date] <> DateMin
)
RETURN
SumOld + SumNew
Worked example PBIX file: https://pwrbi.com/so_54707672/