Compare 2 rows in same table of PowerBI - 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"
)

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:

DAX column count latest record for each set of group with a condition

I want to get Latest updated record which is bit tricky to retrieve using DAX column with power bi
Count -> Order Count based on Modified On(Datetime) with Ascending Order
Deleted -> a Flag set to be True for deleted record
Id
Name
Modified On
Deleted
Count
Result
1
Charles
09-11-2022 15:09:40
1
1
09-11-2022 15:46:33
True
2
1
Charles M
09-11-2022 20:39:40
3
True
2
Charles
09-11-2022 15:09:40
1
2
09-11-2022 15:46:33
True
2
2
Charles M
09-11-2022 20:39:40
3
2
09-11-2022 21:16:33
True
4
2
charles m
09-11-2022 21:18:33
5
3
Dani
09-11-2022 15:46:33
1
True
3
09-11-2022 21:16:33
True
2
4
George
09-11-2022 15:46:33
1
4
George K
09-11-2022 21:16:33
2
In the above example I wanted the Result column values as it is on above table.
explanation:
Here Id : 1, The record is two times created as well as deleted so the history of record will have four rows. I wanted the last updated record which is the 3rd row and not the last record because the is Deleted flag is set to be True so there is no Name on it.
as so on for the second but Id:2
If the last insert on the record is not deleted then whole result column of the id should not return anything
(Id: 3)
In the third set there is there is no update on the record with this history table. the first row is created and second is for the deletion. so we should have to retrieve the first record which only have that data on Name field
Id: 4
There is no deletion operation happened so we don't want to get that record. the result columns should be empty
Thanks in advance
I have tried to get the latest record with
LatestDeletedRecord =
VAR latest = CALCULATE(MAX('Table'[Column3]), ALLEXCEPT('Table','Table'[Id]))
RETURN IF('Table'[Column3] = latest && 'Table'[IsDeleted] = True,True)
Other than nothing I could, I am new to DAX calculations
Edit: If your requirements change, you should perhaps post a new question instead of editing your existing question :-)
With your altered requirements you can use this calculated column:
Result =
VAR _max =
CALCULATE (
MAX ( 'Table'[Modified On] ) ,
ALLEXCEPT ( 'Table' , 'Table'[Id] )
)
VAR _max_is_deleted =
CALCULATE (
SELECTEDVALUE ( 'Table'[Deleted] ) ,
ALLEXCEPT ( 'Table' , 'Table'[Id] ) ,
'Table'[Modified On] = _max
)
VAR _max_mod =
// Calculate the maximum modified date where name is not deleted
CALCULATE (
MAX ( 'Table'[Modified On] ) ,
ALLEXCEPT ( 'Table' , 'Table'[Id] ) ,
'Table'[Name] <> ""
)
RETURN
IF (
// For rows where ID has an associated deletion AND modified is max with name
_max_is_deleted
&& [Modified On] = _max_mod,
// Return "True"
"True"
)
Gives your desired result:

Search the second max value by multiple conditions in power BI

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

How to check if the value is negative within the given time interval in DAX?

I have a value where I want to check if it is negative or 0 within the first 4 weeks from now.
Product ID Quantity Week Ending
A 5 18/07/2021
A -6 25/07/2021
A 4 29/08/2021
B 2 18/07/2021
B 7 25/07/2021
For example for my sample dataset above, product A is negative within the first 4 weeks from now because it is negative on 25/07/2021. So I want to create a measure that gives me Yes or No based on the condition above for all products. In this case,
Finally, when I use the measure in matrix along with the product ID, It should give me result that resembles below:
Product ID Is Short In 4 Weeks
A Yes
B No
Can anyone please help me with this?
You can create a measure with below code.
Negative_Check =
VAR result =
CALCULATE (
MIN ( 'Table (3)'[Quantity] ),
FILTER (
'Table (3)',
'Table (3)'[Week Ending] >= TODAY ()
&& 'Table (3)'[Week Ending]
<= TODAY () + 28
)
)
RETURN
IF ( result < 0, "Yes", "No" )

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/