Power BI how to create new column in date table based on conditions and reference to another table - powerbi

I have a date table called 'dDate', it looks like below:
Date
01/01/2020
02/01/2020
03/01/2020
I have another table called 'Calendar', which looks like below. Note that not all dates are present in this table.
Date Holiday
01/01/2020 1
03/01/2020 0
I want to add a column 'HolidayDate' to the dDate table, where, for a particular date, a value of 1 is given if 'Holiday'=1 from Calendar, else a value of 0. So the HolidayDate column looks for the date in the Calendar table, checks if holiday is 1 and returns 1 if it is.
So the output in dDate table should look like:
Date HolidayDate
01/01/2020 1
02/01/2020 0
03/01/2020 0
I want to add a new column and specify a formula which achieves the above. How can I do this?

If Calendar and dDate has relationship, you can create the following calculated column in dDate
HolidayfromCalendar = RELATED('Calendar'[Holiday])
If Calendar and dDate has no relationship, you can create the following calculated column in dDate
HolidayfromCalendar =
CALCULATE (
MAXX (
FILTER ( 'Calendar', 'Calendar'[Date] = MAX ( dDate[Date] ) ),
'Calendar'[Holiday]
)
)

If you create a simple relationship between you source table with the date and the table with the calendar, then you can use the holiday date for the source table and PowerBI will make the join for you. You don't need to "copy" the holiday indicator across. In fact, the holiday indicator belongs with the calendar table, since this is effectively your date dimension in a Kimball model.
As per comment, if you model is not yours to change, or you have painted yourself in to a bit of a corner with complexity, you can resort to using LOOKUPVALUE to pull the data across. Keep in mind this may have a performance impact on huge data sets.

Related

IF 'AND-OR' ISFILTERED combination in DAX giving problems

Below is the sample dataset
The data has two slicers ( date and category ) shown below
I am writing a DAX Statement to multiply the sum(values) * 10 only if the date range is in the current year 2023.
The StartYear gives the start of the current year, firstD gives the lowest date from the date slicer.
Formula =
var new = sum(Test[Value]) * 10
var startyear = DATE(YEAR(TODAY()),1,1)
var firstD = CALCULATE( MIN( Test[Date]), ALLSELECTED(Test[Date]) )
return if( ISFILTERED(Test[Categories]) && firstD >= startyear, new, 0 )
Now when I filter dates to 2023, the total value should be 2300 but it shows as 0
However the DAX works when I select A or B
If we remove the ISFILTERED function then, it gives wrong value, the expected value is 0 because the start date is in 2022, but it shows 650
let me know if that is the right syntax
It looks like you are not using a separate calendar table to handle this, which you need!
In your very last example you have set your slicer to some time late 2022, but the minimum value of 'Test'[Date] for your selected category is in year 2023. Hint: set the slicer to e.g. 2022-12-14, this will include a 2022-date for Category A in your data.
Your measure behaves exactly how it is supposed to, in other words!
To fix this, you need to do the following:
Create a calendar table in your model, this should contain contiguous dates, which is necessary for the filtering method you want
Establish a relationship between the calendar table and existing Test table.
Use the date column from your new calendar table in your slicer and as date reference in your measure
Exactly how to create a calendar table is thoroughly documented on Google, I recommend you search and find an article or video you understand for implementing this.
Lastly: Your use of ISFILTERED in this measure seems strange, since you mention nowhere the requirement of only showing a number if the column you are testing filtering on is filtered, if that makes sense.. :-) The way you describe your calculation, you only need to check whether the selected date range starts in current year.

Distinct count of values based on date in DAX

I have a table like shown below:
ID
Date
Asset
Location
145
7/29/22
A
Market
145
7/30/22
A
Warehouse
145
7/29/22
B
Market
145
7/29/22
C
Truck
150
7/30/22
B
Market
145
7/29/22
D
Market
145
7/30/22
A
Market
What I am trying to accomplish is to get a distinct count of IDs for each date with a location filter as well. So I would want a count of ID based on the slicer selected Date of 7/29/22 AND 7/30/22 for the Market Location. The desired result is 2 for the selected dates from the date slicer which directly corresponds to the date column in the table.
I was trying to use this DAX formula and wasn't getting anywhere....
IDsMarket =
CALCULATE (
DISTINCTCOUNT ( 'Products'[ID] ),
ALL ( 'Products' )
)
I have a measure dropped onto a card. I should have specified that. My apologies. I need 1 measure to show me the combined count for the two days selected.
I tried this with countrows as well but of course the result wasn't distinct... Any help would be greatly appreciated!!
The formula you're looking for is
IDsMarket =
CALCULATE(
DISTINCTCOUNT('Products'[ID]),
'Products'[Location] = "Market"
)
The resulting Table will look like this
But if you put the measure on a Card visual, you'll get
So in DAX the same measure can yield 1000 different values - depending on the filter context.
I created a conditional column in Power Query and combined the ID with the "day" number from the date column which allowed me to then do a distinct count on that combined custom column which produced to correct answer. Sorry for all the confusion. One of those days.

Calculate Works until i add FILTER

I'm learning DAX and I'm trying to make a measure for Sales Last year.
this formula works :
Sales LY = CALCULATE([Sales CY],SAMEPERIODLASTYEAR(Date[Date]))
But when I add FILTER to the measure, it gives me the values from the current year like "Sales CY"
Sales LY = `CALCULATE([Sales CY], Filter (dim_date, SAMEPERIODLASTYEAR (Date[Date]) ))`
I already have a date filter on the page relative date in this year
the invoices Table and Date table are joined on the date of the creation of the invoice (createdat)
Any ideas what's the meaning behind the things in blue circles?
The FILTER function expects a condition that can be checked for each row of the table instead of a column of dates returned by SAMEPERIODLASTYEAR, so I wouldn't expect the second version to work.
I think the bits circled in blue are emphasizing that you are connected an imported data table to a DirectQuery table (different storage modes).

Power BI - Select Slicer Date Between 2 Columns

Hopefully a quick explanation of what I am hoping to accomplish followed by the approach we've been working on for over a year.
Desired Result
I have a table of SCD values with two columns, SCD_Valid_From and SCD_Valid_To. Is there a way to join a date table in my model (or simply use a slicer without a join) in order to be able to choose a specific date that is in between the two SCD columns and have that row of data returned?
Original Table
ID | SCD_Valid_From | SCR_Valid_To | Cost
1 2020-08-01 2020-08-03 5.00
Slicer date chosen is 2020-08-02. I would like this ID=1 record to be returned.
What We've Attempted So Far
We had a consultant come in and help us get Power BI launched last year. His solution was to create an expansion table that would contain a row for every ID/Date combination.
Expanded Original Table
ID | SCD_Valid_Date | Cost
1 2020-08-01 5.00
1 2020-08-02 5.00
1 2020-08-03 5.00
This was happening originally on the Power BI side, and we would use incremental refresh to control how much of this table was getting pushed each day. Long story short, this was extremely inefficient and made the refresh too slow to be effective - for 5 years' worth of data, we would need over 2000 rows per ID just to be able to select a dimensional record.
Is there a way to use a slicer where Power BI can select the records where that selected date falls between dates in two columns of a table?
Let me explain a workaround and I hope this will help you to solve your issue. Let me guess you have below 2 tables-
"Dates" table with column "Date" from where you are generating the date slicer.
"your_main_table" with with column "scd_valid_from" and "scd_valid_to".
Step-1: If you do not have relation between table "Dates" and "your_main_table", this is fine as other wise you have to create a new table like "Dates2". For this work around, you can not have relation between those tables.
In case you have already relation established between those tables, create a new custom table with this below code-
Dates2 =
SELECTCOLUMNS(
Dates,
"Date", Dates[Date]
)
From here, I will consider "Dates2" as source of your Date slicer. But if you have "Date" table with no relation with table "your_main_table", just consider "Dates" in place of "Dates2" in below measures creation. Now, Create these following 4 measures in your table "your_main_table"
1.
date_from_current_row = max(join_using_date_range[SCD_Valid_From])
2.
date_to_current_row = max(join_using_date_range[SCD_Valid_to])
3.
date_selected_in_slicer = SELECTEDVALUE(Dates2[Date])
4.
show_hide_row =
if(
[date_selected_in_slicer] >= [date_from_current_row]
&& [date_selected_in_slicer] <= [date_to_current_row]
,
1,
0
)
Now you have all instruments ready for play. Create your visual using columns from the table "your_main_table"
Final Step: Now just add a visual level filter with the measure "show_hide_row" and set value will show only when "show_hide_row = 1".
The final output will be something like below image-

Power bi - Use Slicer selection in filtering data

I am new to Power bi but i want to use Slicer(user selection) while filter data with comparison by 'Greater Than' instead of equal to.
I have data of devices with LastUpdated date column. and slicer with few list of dates for 15 days gap (calendar would be better but as it is not available yet sticking with dates list)
When user select a date in Slicer i want to filter the data whose Lastupdated Date is greater than equal to selected date. How to achieve this? tried columns,measures..
Any help is appreciated.
I think you can create a measure that links the Date list table and the Device table, even if there is no relationship between them. However the measure must be present in your visualization otherwise the slicer will not affect it.
I've created a measure that calculates the maximum date for those rows which last update date is greater or equal than the slicer selection.
CalculatedLastUpdate =
CALCULATE (
MAX ( DeviceTable[LastUpdate] ),
FILTER (
DeviceTable,
DeviceTable[LastUpdate] >= MINX ( DateList, DateList[Date] )
)
)
DateList - a table containing a column [Date] with you date range.
DeviceTable - a table containing device data.
Now you can delete LastUpdate column from your visualization in order to avoid two columns with the same data.
Let me know if it helps.
I don't know about earlier , but now you can modify the date slicer to do as "after" the given date (you can do so by clicking on the icons present in the rightmost side of the slicer visual itself , wher you can select mode of slicer as between , after ,list , dropdown etc.)...which I believe means that you get all data for dates greater than the selected dates for the given column used in slicer which in your case will be LastUpdate.