Create a measure to return latest value - powerbi

I am trying to retrieve the latest job designation of each ID and transaction with blank field.
The current code that I am using for creating a new measure
Result =
var i = SELECTEDVALUE('SampleData'[ID])
var d =
maxx(
filter(
all('SampleData'),
'SampleData' [ID] = i
&& 'SampleData' [Job Designation] <> BLANK()
),
'SampleData' [TS]
)
var de =
maxx(
filter(
all('SampleData'),
'SampleData' [ID] = i
&& 'SampleData' [aTS] = d
),
'SampleData' [Job Designation]
)
return de
Please refer to my sample data below.
enter image description here
As you refer to the sample data, I have "Update" transaction which involves updating of company details only. Therefore, user need not provide ID. The query that I currently have will remove the ones with blank ID.
The desired outcome is to retrieve the latest job designation for each ID and return blank if ID is empty, but Company ID has data (i.e. 852 and 654).
Please help me out.
Thank you in advance.

If I understand you correct, you need this below measure for your purpose-
latest_designation =
MAXX(
TOPN(
1,
FILTER(
ALLSELECTED('SampleData'),
'SampleData' [Job Designation] <> BLANK()
),
'SampleData' [TS],
DESC
),
'SampleData' [Job Designation]
)
As you have slicer for selecting ID, you do not need filter table using Slicer Value insider the measure.

I have found the answer.
I didn’t select “Show items with no data”.

Related

DAX RANKX Within Group

I have a table called CarSales with the following sample data:
I already have a measure that ranks all Brands by sales:
Rank = RANKX(ALL('CarSales'[Brand]),[NetSales])
However, I need another measure/column **"Rank(Country)" ** which ranks the Brands within the Country group (without having to display the Country group in the table)
Thanks
Try the following measure:
=
VAR ThisCountry =
MIN( CarSales[Country] )
RETURN
RANKX(
FILTER( ALLSELECTED( CarSales ), 'CarSales'[Country] = ThisCountry ),
[NetSales]
)

DAX - extract the date belongs to latest snapshot for each group into a new column

I need to create a new column showing the "expiration date" that belongs to the latest snapshot for each group.
Please help with the DAX query for this new calculated column.
Desired output:
Thanks
Create a calculated column called ID
ID = Contracts[Contract ID] & "-" & Contracts[Year Month]
Then, two measures
MAX ID =
CALCULATE(
MAX(Contracts[ID]),
ALLEXCEPT(Contracts,
Contracts[Contract ID]
)
)
and
Latest Expire Date = LOOKUPVALUE(Contracts[Expiration Date], Contracts[ID], [MAX ID])
For your case one column is enough ).
=
VAR myID=[Contract ID]
VAR onlyGroup=
FILTER(
'yourTableName'
,'yourTableName'[Contract ID] = myID
)
VAR lastSnSt= MAXX(onlyGroup,[Year Month])
RETURN
CALCULATE(
MAXX(onlyGroup,[Expiration Date])
,'yourTableName'[Year Month]=lastSnSt
)

Power BI how to add a new row based on condition after group

I have a table like this:
enter image description here
I want to create a default value for each group(each month should have a default amount 0), I know we should do group function first, but I do not know want to do next, very appreciate who give me help
I'm not sure exactly where you are going with your request. I understand it as: Adding a row for the first day of each month with a default value of zero.
This is not easy DAX. It's a bit complicated, so feel free to provide more info about your need, there may be another way more simple way.
The first step is to generate a table of the first day of each month. After a quick search, I've found a way in another answer and I was able to modify it a bit.
TABLE2 =
SELECTCOLUMNS(
FILTER(
ADDCOLUMNS(
CALENDAR(
DATE(2022, 01, 01),
DATE(2022, 12, 01)
),
"IsFDOM",
IF(
DAY([Date]) = 1,
TRUE(),
FALSE()
)
),
[IsFDOM] = TRUE()
),
"DATA",
[Date],
"AMOUNT",
0
)
What it does:
It generate a calendar between two dates I've arbitrary set : January 1st to Decembre 1st.
It adds a columns in this generated table IsFDOM (Is First Day Of Month) that return True is it is.
This table is then filtered to keep only the rows where IsFDOM is True.
It is wrapped in a SELECTCOLUMNS to keep and rename the columns you want and to match your existing table.
Next, you want to merge your existing table with this newly generated table. The DAX function to do it is UNION and requires both table to have the same format - i.e same number of columns.
TABLENEW =
UNION(
TABLE1,
FILTER(
TABLE2,
NOT TABLE2[DATA] IN VALUES(TABLE1[DATA])
)
)
I've named you existing table TABLE1 in this example.
The FILTER is used to not merge rows (dates) that already exist in TABLE1.
And, to make things even nicer. We can bypass the creation of TABLE2 by embedding it in the table expression of TABLENEW :
TABLENEW =
VAR MaxDate =
MAX(TABLE1[DATA])
VAR TMP_Calendar =
FILTER(
SELECTCOLUMNS(
SUMMARIZE(
FILTER(
ADDCOLUMNS(
CALENDAR(
DATE(2022, 01, 01),
DATE(2022, 12, 01)
),
"IsFDOM",
IF(
DAY([Date]) = 1,
TRUE(),
FALSE()
)
),
[IsFDOM] = TRUE()
),
[Date],
"default",
0
),
"DATA",
[Date],
"AMOUNT",
[default]
),
NOT [DATA] IN VALUES(TABLE1[DATA]) &&
[DATA] <= MaxDate
)
RETURN
UNION(
TABLE1,
TMP_Calendar
)
The calendar formerly known as TABLE2 is now created in a variable TMP_Calendar.
To make thing nicer, MaxDate calculate the max date of your existing table and only merge for existing date.

DAX Using Countrows and Filter to compare value in the same table

I'm freshly new to DAX & PowerBI, I tried to create a measure column that will count the number of child of each parent has. 
The table is something like this (please understand this table structure might be not ideal, but I can't change the existing).
What I expect is to create a new column that will count how many "child" that the "parent" has based on "Parent ID". Like this.
I've tried using this formula but it returns error
Childcount =
COUNTROWS(
FILTER(
Table,
FILTER(
Table,
Table[Parent ID] <> BLANK()
) = Table[ID]
)
Thank you for your help.
This can be done. Just a quick note, there are two types of calculations you canmake in Power BI: measure and calculated column. I Guess you're in nedd of a calculated column so here's a solution using calculated columns.
First create a calculated column like this:
Parent ID =
var idcol = [id]
var result =
IF(
[Type] = "Child",
CALCULATE(
MAX('Data'[ID]),
FILTER(
ALL('Data'),
'Data'[Type] = "Parent" && 'Data'[ID] < idcol
)
),
BLANK()
)
return
result
Then you can create a second calculated column like this:
Childcount =
var idcol = [ID]
return
CALCULATE(
COUNT(Data[Parent ID]),
FILTER(
ALL(Data),
'Data'[Parent ID] = idcol
)
)+0
Since you're new to Power BI/Dax I would highly recomend you check youtube and sqlbi for free introductory videos. Before you understand the concept of how calculations are performed in Power BI you're gonna have a tough time. But it's worth the time investment!

How to note if an indicated action has any future actions occur after it in Power BI

I have a single table with the following columns: User ID, Activity Date, and an indicator column. I'm trying to note if any rows with the indicator have other indicated actions occur after them grouped by each individual user.
Below is an example of what the table looks like and what the desired output (calculated column) is:
Any help on this DAX problem would be greatly appreciated. Thank you
Check the for the latest indicated date for the User ID and check if it's after the current one.
HasLaterIndicators =
VAR LatestDate =
CALCULATE (
MAX ( Table[Activity Date] ),
ALLEXCEPT ( Table, Table[User ID] ),
Table[Indicator] = "X"
)
RETURN
IF ( Table[Indicator] = "X" && Table[Activity Date] < LatestDate, "Yes" )