DAX to lookup date in ValidFrom column - powerbi

I have two (2) tables
Tablename: X
ID Name ValidFrom Property
A-----Test1-----01.01.2010---------30
A-----Test1-----01.01.2015---------60
B-----Test1-----01.01.1900---------30
B-----Test2-----01.01.2018---------60
Tablename: Y
ID Date
A---01.01.2010
A---01.02.2010
A---01.03.2015
A---01.04.2015
Ideally, I would like to add calculated columns to Table Y which looks up ID and date with ID and ValidFrom from Table X. In this example, row#1 in Table X would be the returning row of data for all dates >= 01.01.2010 and dates < 01.01.2015. The resulting outcome would be like this:
Tablename: Y (new)
ID Date Name Property
A---01.01.2010----Test1------30
A---01.02.2010----Test1------30
A---01.03.2015----Test1------60
A---01.04.2015----Test1------60
Any help would be greatly appreciated

It's not clear how the Name column is generated, but here's how you can get the Property column once you have the Name column in table Y:
Property =
VAR LastValid =
CALCULATE (
MAX ( X[ValidFrom] ),
FILTER (
ALL ( X[ValidFrom] ),
X[ValidFrom] <= EARLIER ( Y[Date] )
)
)
RETURN
LOOKUPVALUE (
X[Property],
X[ID], Y[ID],
X[Name], Y[Name],
X[ValidFrom], LastValid
)
The LastValid variable finds the latest date that is less than or equal to the date in the current row. Then you use that along with the ID and Name to look up the Property from table X.

Taking in the last formula and applying the LOOKUPVALUE to the ID and ValidFrom as you refers, I tried successfully the next approach:
Name =
VAR LastValid =
CALCULATE(MAX(X[ValidFrom]);
FILTER (
ALL(X[ValidFrom] );
X[ValidFrom] <= EARLIER (Y[Date] )
)
)
RETURN
LOOKUPVALUE (
X[Name];
X[ID]; Y[ID];
X[ValidFrom]; LastValid
)

Related

Dax - How to get the max value from a column compare with Selected date and row by row

My Dax dont work..
ERROR:
A single value for column 'Datum' in table 'tabell' cannot be determined. This can happen when a measure formula refers to a column that contains many values without specifying an aggregation such as min, max, count, or sum to get a single result.
Q:
How to i get the desired values from my dax to get the latest date?
I want it to return the newst date but if its older then the selected values i want it to return that value. I have done a dax but you see the error above.
I want every row to return the Datum or the selected values depedning on the logic.
Selected values '2022-09-01'
desired value = MAX('tabell'[Datum],selectedvalue(vwdatum[datum].[date]))
Datum desired value
2020-09-25 2020-09-25
2020-09-22 2020-09-25
2020-01-02 2020-09-01
alternativ
IF('tabell'[Datum]>selectedvalue(vwdatum[datum].[date]),'tabell'[Datum],selectedvalue(vwdatum[datum].[date])
Maybe It is much better to create a calculated column, and use this DAX code to evaluate it on a row_by_row basis:
Just write your selected date value into the variable:
Desired Value =
VAR SelectedValue =
DATE ( 2020, 09, 01 )
RETURN
IF ( Tabell[Datum] > SelectedValue, MAX ( Tabell[Datum] ), SelectedValue )
If we test it, It gives us:
SelectedEnd Code For #Jonas
SelectedEnd =
VAR aret =
SELECTEDVALUE ( 'vwDimDatum2 Slut'[År],2020 )
VAR manad =
SELECTEDVALUE ( 'vwDimDatum2 Slut'[Månad nr], 01 )
VAR datumet =
DATE ( aret, manad, 01 )
VAR sista =
EOMONTH ( datumet, 0 )
RETURN
sista

How to get the value at the latest date, where values are not null?

I have a table like this. Notice the blank values towards the bottom.
I would like to get the value of the latest date, but the value can't be null.
My DAX formula is bringing back the value of the latest date (19/12/2021) which is nullhowever I want to bring back the latest non-null value, which is for the date 21/11/2021.
Here is what I have tried so far:
Latest Value =
CALCULATE(
// get sum of value column
SUM('Table1'[Value]),
// where value is not blank, and date is max date
'Table1'[Value] <> BLANK() && Table1[Date] = MAX(Table1[Date])
)
I thought this should bring back the figure 305? Because my conditions are:
where value is not null AND where date = max date
Shouldn't the max date now be 21/11/21 because the nulls have been removed?
Another piece of DAX I've tried, using the fiter function.
Latest Value = CALCULATE(
SUM('Table1'[Value]),
FILTER(ALL('Table1'),
'Table1'[Value] <> BLANK()
&&
'Table1'[Date] = MAX('Table1'[Date]))
Where am I going wrong? I think it's something to do with my max date section.
Unfortunately all file hosters are blocked in work, so I can't share this dummy file.
The idea is to filter the table first and get the max value from the date column. In my case, I saved that date in a variable last_date. Then we just select a value from the Value column using filter by last_date.
LatestValue =
VAR last_date =
CALCULATE ( MAX ( 'Table1'[Date] ), 'Table1'[Value] <> BLANK () )
RETURN
CALCULATE ( SELECTEDVALUE ( 'Table1'[Value] ), 'Table1'[Date] = last_date )
or the same expression with SUM:
LatestSumOfValues =
VAR last_date = CALCULATE(MAX('Table1'[Date]),'Table1'[Value] <> BLANK())
RETURN
CALCULATE(SUM('Table1'[Value]),'Table1'[Date] = last_date)

How to show not filtered data

I am new to power bi, and this question might be a common question and already answered, but I didn't find any precise solution.
I have two tables; "Dates" and "Accounts".
Dates table has only one column: "Date"(date type). It has only date value, which is day based.
Accounts table has two columns; Name(text type) and CreatedDate(date dypttype).
In my power bi model, there is a relationship(many to one, single, active) between Dates.Date and Accounts.CreatedDate columns.
I want to show account names that except the filtered ones. For example, my Accounts table look like this:
Name
CreatedDate
A Company
2020-01-01
B Company
2020-12-15
C Company
2021-03-03
D Company
2019-05-27
I have a slicer and use Dates.Date as It's field.
When I filtered my data as last 1 year (2020-08-18 - 2021-08-18), I want to show the data which is not filtered. I want to see this:
Name
CreatedDate
A Company
2020-01-01
D Company
2019-05-27
How to show the data which is not filtered?
You can create a filter Measure like following
_filter:=
SWITCH (
TRUE (),
/* t1 = what is value selected ?*/
CONVERT ( SELECTEDVALUE ( 'Calendar'[Calendar_Date] ), INTEGER ) = BLANK ()
/* t2=what is the max CreatedDate value visble in the filter context*/
/*if t1 and t2 both blank then return -999 else return t1-t2 which should be 0*/
&& CONVERT ( MAX ( tbl[CreatedDate] ), INTEGER ) = BLANK (), -999,
CONVERT ( SELECTEDVALUE ( 'Calendar'[Calendar_Date] ), INTEGER )
- CONVERT ( MAX ( tbl[CreatedDate] ), INTEGER )
)

PowerBI: How to get distinct count for a column in a table, while grouping for many columns separately

I have a table with multiple date columns, and a single label column, as shown by following code
Data = DATATABLE (
"Date1", DATETIME,
"Date2", DATETIME,
"Label", STRING,
{
{ "2020-01-01","2020-01-02", "A" },
{ "2020-01-01","2020-01-01", "A" },
{ "2020-01-01","2020-01-02", "B" },
{ "2020-01-01","2020-01-01", "D" },
{ "2020-01-01","2020-01-02", "E" },
{ "2020-01-02","2020-01-01", "A" },
{ "2020-01-02","2020-01-02", "B" },
{ "2020-01-02","2020-01-01", "C" }
}
)
I want to plot a chart of count of distinct labels for each day, when considering date1, as well as when considering date2. These need to be in same plot, as a clustered bar plot, as shown below. This means I need to get the values on a new date column.
The expected result looks like this,
Date | value1 | value2
---------------------------------
1/1/2020 12:00:00 AM | 4 | 3 |
1/2/2020 12:00:00 AM | 3 | 3 |
Current Solution:
I am creating two different tables for each of the counts, as follows
Date1_Count =
ADDCOLUMNS (
ALL ( Data[Date1] ),
"Count",
CALCULATE (
DISTINCTCOUNT ( Data[Label] )
)
)
and
Date2_Count =
ADDCOLUMNS (
ALL ( Data[Date2] ),
"Count",
CALCULATE (
DISTINCTCOUNT ( Data[Label] )
)
)
Then I create a third table with dates as such,
Final_Counts = CALENDAR("2020-01-01", "2020-01-04")
Next, I add relationship between the three dates, viz. Date1_Count table, Date2_Count table, and Final_Counts table
Finally, I combine the data using RELATED function as follows
value1 = RELATED(Date1_Count[Count])
value2 = RELATED(Date2_Count[Count])
Question
Is there a simpler solution that does not require creating one table per date column? The current method is not scalable to many date columns.
Assuming you only have a handful of date columns, you just need a single date dimension table and one measure per date column.
Define a date table to use on the x-axis (no relationships to other tables):
DimDate = CALENDAR("2020-01-01", "2020-01-04")
Then define measures that match the various date columns to the date table:
value1 =
CALCULATE (
DISTINCTCOUNT ( Data[Label] ),
Data[Date1] IN VALUES ( DimDate[Date] )
)
and
value2 =
CALCULATE (
DISTINCTCOUNT ( Data[Label] ),
Data[Date2] IN VALUES ( DimDate[Date] )
)
If you have more than a handful of DateN columns, then you'd probably be best served to reshape your data where you unpivot all those columns.
For just the two you have the data would look like
In this case, you use Unpivot[Column] as the Legend and only need a single measure:
value =
CALCULATE (
DISTINCTCOUNT ( Unpivot[Label] ),
Unpivot[Date] IN VALUES ( DimDate[Date] )
)
This gives a similar looking result:
It is possible to obtain the Final_Counts calculated table in one step, using ADDCOLUMNS to iterate over Data[Date1], and then calculating Value1 as the DISTINCTCOUNT over the Data table filtered on the currently iterated Date1.
This work thanks to the CALCULATE statement that triggers a context transition.
Obtaining the Value2 requires to create a new filter context over Date2 using the currently iterated Date1.
First we save the current Date1 in a variable to be used inside CALCULATE in the filter expression on Date2.
We also need REMOVEFILTERS( Data ) to remove the filter context over Date1 set by the context transition.
Final_Counts =
ADDCOLUMNS(
ALL( Data[Date1] ),
"Value1",
CALCULATE(
DISTINCTCOUNT( Data[Label] )
),
"Value2",
VAR CurrentDate = Data[Date1]
RETURN
CALCULATE(
DISTINCTCOUNT( Data[Label] ),
REMOVEFILTERS( Data ),
Data[Date2] = CurrentDate
)
)

DAX create empty table with specific column names and no rows

How to create a table with a specified column name and no rows at all. The following oneliner does what I want but shows error message that there should be second argument in ROW function.
EmptyTable = ROW ("Product")
I would like to use it for making bridge tables with desired column name. For example I want Product_bridge table to have a column "Product".
Product_bridge = DISTINCT(
UNION(
DISTINCT( Sales[Prod_Name] )
,DISTINCT( Dictionary[Prod_DifferntName])
,DISTINCT( PriceList[P] )
))
The code above gets me the name of the first table, in this case Prod_Name.
You could just filter it. Or select TOPN 0.
TOPN:
Table = TOPN(0;DATATABLE("Product";STRING;{{}}))
FILTER:
Table = FILTER(DATATABLE("Product";STRING;{{}});0)
This is how I create empty DAX tables:
EmptyTable = DATATABLE (
"Other Measures", INTEGER,
{
{ 0 }
}
)
I would like to add to mxix answer a few useful oneliners for making one-column empty table with desired name:
OneLiner1 = TOPN(0, ROW("Product", "Apple"))
OneLiner2 = FILTER(ROW("Product", "Apple"), 1=2)
Or if you want to define column type:
OneLiner3 = TOPN(0, DATATABLE("Product", STRING,{{"Apple"}}) )
So the snipped for bridge table is:
Product_bridge = DISTINCT(
UNION(
TOPN(0, ROW("Product", "Apple"))
,DISTINCT( Sales[Prod_Name] )
,DISTINCT( Dictionary[Prod_DifferntName])
,DISTINCT( PriceList[P] )
))