I need to get the field "Name" from "Table1" in a calculated column in "Table2", like:
Table1:
Name | Date
ABC | 5-jan-2017
ABC | 7-jan-2017
DEF | 8-may-2018
DEF | 10-jun-2018
And Table2:
Date | CalcColumn
6-Jan-2017 | ABC
25-may-2018 | DEF
The logic is the following:
If Date in Table2 is within the minimum and maximum dates of Table1 then get the name in Table1.
I'd recommend reshaping Table1 so that each Name has two columns, StartDate and EndDate instead of having those in separate rows. If you don't, you can create that table as a variable as follows:
CalcColumn =
VAR Summary =
SUMMARIZE (
Table1,
Table1[Name],
"StartDate", MIN ( Table1[Date] ),
"EndDate", MAX ( Table1[Date] )
)
RETURN
MAXX (
FILTER (
Summary,
Table2[Date] >= [StartDate] &&
Table2[Date] <= [EndDate] ),
Table1[Name]
)
You only need the part after the RETURN if your data is reshaped.
Related
I'm trying to imitate this report (page 3) where it slices active headcount and all the other metrics (1) by date and (2) by department.
My data looks like this (with relationships, of course):
ID
Name
DEPID
Hired Date
Terminated Date
Terminated (Y/N)
1
John
2
1/1/2019
2020/12/31
Y
2
Jane
2
1/3/2018
2019/07/26
Y
3
Jack
1
1/5/2022
null
N
Using the following measure, I was able to extract total number of employees by date, but I wasn't able to filter by department:
CountOfActive =
var _selectedDate = MAX('Calendar'[Date])
return
CALCULATE(COUNTROWS('Table'); filter(ALL('Table'); Table[HIREDDATE] <= VALUE(_selectedDate) && (Table[TERMINATEDDATE] >= VALUE(_selectedDate) || ISBLANK(Table[TERMINATEDDATE]))))
My ideal output is something like the following (where I'll create a table for each department and list the number of active employees, then join them to my department key table afterwards so I can slice them):
Date
Count of Active Employees
Department
2019/1/1
3
Retail
2019/1/2
3
Retail
2019/1/3
4
Retail
...
...
...
The "Date" column would be a calendar table built with CALENDAR().
What should I do to achieve the last table based on the data I have?
My relationship schema looks like this.
try this : 'Table 2' is your Calendar Table which is also a slicer on the visual.
Make sure that your Calendar Table's Date has a relation with the Hired Date and also the relation between the Department Table
Count of Emp =
VAR _latest =
MAX ( 'Table 2'[Date] )
VAR _from =
MIN ( 'Table 2'[Date] )
VAR _dept =
SELECTEDVALUE ( Department[Department] )
RETURN
CALCULATE (
COUNTX ( 'Table', 'Table'[ID ] ),
FILTER (
ALL ( 'Table' ),
'Table'[Terminated Date ] >= _from
&& 'Table'[Hired Date ] <= _from
&& 'Table'[Terminated (Y/N)] = "Y"
&& RELATED ( Department[Department] ) = _dept
)
)
+ CALCULATE (
COUNTX ( 'Table', 'Table'[ID ] ),
FILTER (
ALL ( 'Table' ),
'Table'[Terminated (Y/N)] = "N"
&& 'Table'[Hired Date ] <= _from
&& RELATED ( Department[Department] ) = _dept
)
)
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
)
)
I have a Power BI report and I want to find the last date some action took place that had a non-0 value. In the following example:
|-------------|------------|
| ActionDate | ActionAmt|
|-------------|------------|
| 1-Feb | -100 |
|-------------|------------|
| 1-Feb | 100 |
|-------------|------------|
| 10-Jan | 150 |
|-------------|------------|
I want to return the 10-Jan date, not 1-Feb, since 10-Jan is the first non-0 value summed by ActionDay. My code returns 1-Feb:
Last Action Date =
VAR maxdatekey =
CALCULATE ( MAX ( 'Action'[ActionDateKey] ), 'Action'[ActionAmount] > 0 )
RETURN
IF (
maxdatekey,
FORMAT (
LOOKUPVALUE ( 'Date'[DateValue], 'Date'[DateKey], maxdatekey ),
"dd-MMM-yyyy"
)
)
How do I further group this to exclude the summarized 0 days?
I think you're looking for something like this where you summarize by date when calculating the amount:
LastActionDate =
VAR Summary =
SUMMARIZE (
'Date',
'Date'[DateValue],
"Amt", CALCULATE ( SUM ( 'Action'[ActionAmount] ) )
)
RETURN
FORMAT (
MAXX ( FILTER ( Summary, [Amt] > 0 ), 'Date'[DateValue] ),
"dd-MM-yyyy"
)
I'm working with PowerBI and have the following table:
customer_id|item_id| date
1 | A | 01/01/01
1 | B | 01/01/01
1 | A | 02/02/02
1 | A | 03/03/03
2 | A | 03/03/03
2 | C | 03/03/03
...
I would like to find the earliest date for each customer_id who purchased item A and return 1 in a new column. So that I get a new column in the table that looks like the following:
customer_id | item_id | date | Column_want
1 | A | 01/01/01 | 1
1 | B | 01/01/01 | blank
1 | A | 02/02/02 | blank
1 | A | 03/03/03 | blank
2 | A | 03/03/03 | 1
2 | C | 03/03/03 | blank
...
I've tried to filter the column by item A and then using TOPN(1,...) to choose only the top rows. However, it doesn't seem to work.
This seems like such a trivial request. Is there any smarter way around this?
It's possible to use TOPN for this but that function returns an entire row of a table so it looks pretty clunky like this:
Column_want =
IF (
Table1[item_id] = "A" && Table1[date]
= SELECTCOLUMNS (
TOPN (
1,
FILTER (
Table1,
Table1[item_id] = "A"
&& Table1[customer_id] = EARLIER ( Table1[customer_id] )
),
Table1[date], ASC
),
"date", Table1[date]
),
1
)
I'd suggest something more like this:
Column_Want =
IF (
Table1[date]
= CALCULATE (
MIN ( Table1[date] ),
FILTER (
ALLEXCEPT ( Table1, Table1[customer_id], Table1[item_id] ),
Table1[item_id] = "A"
)
),
1
)
Or this:
Column_Want =
IF (
Table1[date]
= MINX (
FILTER (
Table1,
EARLIER ( Table1[item_id] ) = "A"
&& Table1[customer_id] = EARLIER ( Table1[customer_id] )
),
Table1[date]
),
1
)
You could create a calculated column using variables:
Column_want =
VAR Customer_id ='Table'[customer_id]
VAR Earliest_date = CALCULATE(MIN('Table'[date]),
FILTER('Table','Table'[customer_id]=Customer_id))
VAR Earliest_item = CALCULATE(MIN('Table'[item_id]),
FILTER('Table','Table'[date]=Earliest_date),
FILTER('Table','Table'[customer_id]=Customer_id))
RETURN IF('Table'[date]=Earliest_date && 'Table'[item_id]=Earliest_item,
1,BLANK())
The idea is to calculate the earliest date for a particular Customer ID using Calculate and max (Earliest_date variable). Earliest_Item variable is calculated to avoid multiple records for the same customer getting tagged as 1. Hope this helps.
I have following Data Structure
Date | Category | Sales Amount
----------------------------------------------------
01-Sep-2016 | Food | 100
02-Sep-2016 | Food | 120
03-Sep-2016 | Food | 130
01-Sep-2016 | Electricity | 180
02-Sep-2016 | Electricity | 60
01-Sep-2016 | Perfumes | 80
02-Sep-2016 | Perfumes | 40
I want to calculate the Two Week Sales for Each Category, I might add another column like Territory as well in the future. I used following Formula which worked fine if I only select Date but Does Not Work if I select Category.
SalesTwoWeeksAgo =
CALCULATE (
SUM ( 'Table'[SalesAmount] ),
FILTER (
ALL ( 'Table' ),
COUNTROWS (
FILTER (
'Table',
EARLIER ( 'Table'[Date] ) = DATEADD ( 'Table'[Date], -14, DAY )
)
)
)
)
The Above Formula was contributed by alejandro zuleta and link is
Power BI getting 2 week back same day value
If I understand your question, the problem is that you have a Category column so you need to get the sales two weeks back in the time in the current category value evaluated in the expression. You just have to add an additional condition in the FILTER function to take the current Category and the current Date substracting 14 days, then it will return the related Sales Amount values to the SUM function.
SalesTwoWeeksAgo =
CALCULATE (
SUM ( 'Table'[Sales Amount] ),
FILTER (
ALL ( 'Table' ),
COUNTROWS (
FILTER (
'Table',
EARLIER ( 'Table'[Date] ) = DATEADD ( 'Table'[Date], -14, DAY )
&& 'Table'[Category] = EARLIER ( 'Table'[Category] )
)
)
)
)
Also if you add Territory column to your table you may need to get the Sales Amount two weeks before per Date, Category and Territory so you just need to add a third conditional in the FILTER function.
SalesTwoWeeksAgo =
CALCULATE (
SUM ( 'Table'[Sales Amount] ),
FILTER (
ALL ( 'Table' ),
COUNTROWS (
FILTER (
'Table',
EARLIER ( 'Table'[Date] ) = DATEADD ( 'Table'[Date], -14, DAY )
&& 'Table'[Category] = EARLIER ( 'Table'[Category] )
&& 'Table'[Territory] = EARLIER ( 'Table'[Territory] )
)
)
)
)
The solution provided here is not tested yet but hopefully it is what you need.
Let me know if this helps.