I am new to DAX and i want to do a flag based on the time
the flag column is called "New", to determine whether is it new, Value YTD >0 and Value YTD Past Year is 0 or blank
my table
period | product | value YTD | value YTD Past Year |
202201 | Apple | 1000 | 0
202201 | Orange | 1000 | 0
202201 | Durian | 2033 | 100
output
table
period | product | value YTD | value YTD Past Year | New
202201 | Apple | 1000 | 0 | 1
202201 | Orange | 1000 | 0 | 1
202201 | Durian | 2033 | 100 | 0
Any help is appreciated!
My understanding is that you have three cases :
Value YTD
Value YTD Past Year
Output
>0
>0
0
>0
0
1
0
0
BLANK
Then, you should be able to create the new calculated column New as
New =
SWITCH(
TRUE(),
[Value YTD] > 0 && [Value YTD Past Year] > 0,
0,
[Value YTD] > 0 && [Value YTD Past Year] = 0,
1,
BLANK()
)
Related
I have a measure which displays number of employees in relation to the date.
Each day the FactEmployee is updated to reflect who is working. this means that my measure (obviously) can't display how many employees there are tomorrow.
I would like to persist the latest value (ie. todays value) into the future.
Data model
My (not perfect) measure
Count, employee :=
VAR today = TODAY()
VAR res =
IF (
MAX ( DimDate[fulldate] ) > today,
CALCULATE (
COUNT ( DimEmployee[emp_key] ),
FILTER ( ALL ( FactEmployee ), RELATED ( DimDate[fulldate] ) = today)
),
CALCULATE ( COUNT ( DimEmployee[emp_key] ), FactEmployee )
)
RETURN
res
Output
year-month count, emp
---------------------------
2020-01 182
2020-02 180
2020-03 174
2020-04 171
2020-05 171
2020-06 173
2020-07 172
2020-08 175
2020-09 172
Expected Output
year-month count, emp
--------------------------
2020-01 182
2020-02 180
2020-03 174
2020-04 171
2020-05 171
2020-06 173
2020-07 172
2020-08 175
2020-09 172
2020-10 172 <----repeated value from 2020-09
2020-11 172 <----repeated value from 2020-09
2020-12 172 <----repeated value from 2020-09
how can i fix my measure to get the missing values (oktober to december)?
I have replicated your model using a simplified version, I don't think you need dimEmployee in this case.
Assuming your model is like this
And your tables look like these:
FactEmployee
+----------+---------+
| date_key | emp_key |
+----------+---------+
| 20200101 | 1 |
+----------+---------+
| 20200102 | 1 |
+----------+---------+
| 20200103 | 1 |
+----------+---------+
| 20200104 | 1 |
+----------+---------+
| 20200105 | 1 |
+----------+---------+
| 20200101 | 2 |
+----------+---------+
| 20200102 | 2 |
+----------+---------+
| 20200104 | 2 |
+----------+---------+
| 20200101 | 3 |
+----------+---------+
| 20200102 | 3 |
+----------+---------+
| 20200103 | 3 |
+----------+---------+
| 20200104 | 3 |
+----------+---------+
| 20200105 | 4 |
+----------+---------+
DimDate
+------------+----------+
| Date | Date_key |
+------------+----------+
| 01/01/2020 | 20200101 |
+------------+----------+
| 02/01/2020 | 20200102 |
+------------+----------+
| 03/01/2020 | 20200103 |
+------------+----------+
| 04/01/2020 | 20200104 |
+------------+----------+
| 05/01/2020 | 20200105 |
+------------+----------+
| 06/01/2020 | 20200106 |
+------------+----------+
| 07/01/2020 | 20200107 |
+------------+----------+
I have created a calculation that follow these steps:
Compute the maximum date with valid or non blank values for the distinct count of emp key, under the variable MaxDateKey.
IF statement evaluated for date_key greater than 'MaxDatekey' - in this case 20200106 and 20200107. For those dates, the calculation retrieves the distinct count of emp_key for MaxDateKey.
When the IF stamenet is false, distinct count is calculated as usual.
Count =
VAR MaxDateKey =
CALCULATE (
LASTNONBLANK ( FactEmployee[date_key], DISTINCTCOUNT ( FactEmployee[emp_key] ) ),
REMOVEFILTERS ( DimDate[Date] )
)
VAR Result =
IF (
MAX ( DimDate[Date_key] ) > MaxDateKey,
CALCULATE (
DISTINCTCOUNT ( FactEmployee[emp_key] ),
ALL ( DimDate[Date] ),
DimDate[Date_key] = MaxDateKey
),
DISTINCTCOUNT ( FactEmployee[emp_key] )
)
RETURN
Result
The output below. The values from the last valid date 5th of Jan is applied to the subsequent dates (6th and 7th of Jan).
For line chart, you can check the Forecast option from the Analytics pane as shown below.
The output will be something like below-
I've 2 data sets, one which represts a list of all of the customers and other with their order dates
The order date are in a yyyyweek_number format, so for instance as today (2020-09-29) is week 40, the order date would be represented as 202040
I want to get a list of dealers who haven't placed orders in 4 day ranges viz. 30 days or less
60 days or less
90 days or less and
90+ days
To illustrate lets say the customer dataset is as under:
+----+
| ID |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 10 |
| 11 |
| 12 |
| 13 |
| 14 |
| 15 |
+----+
and the Order table is as under:
+----+-----------------+
| ID | Order_YYYY_WEEK |
+----+-----------------+
| 1 | 202001 |
| 2 | 202003 |
| 3 | 202004 |
| 5 | 202006 |
| 2 | 202008 |
| 3 | 202010 |
| 6 | 202012 |
| 8 | 202009 |
| 1 | 202005 |
| 10 | 202015 |
| 11 | 202018 |
| 13 | 202038 |
| 15 | 202039 |
| 12 | 202040 |
+----+-----------------+
The slicer format that I've looks like this
Now say for instance the 30 days or less button is selected,
the resulting table should represent a table as under, with all the ID's from the Customer table that aren't present in the ORDER table where ORDER_YYYY_WEEK is 30 days from todays week
+----+
| ID |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 10 |
| 11 |
| 14 |
+----+
Steps:
Create relationship between Customer id's in Customer table and Order table (if not already there)
Create a Date table
Convert Weeks to dates in a new calculated column in the Order table
Create relationship between Customer id's in Customer table and Order table
Create relationship between Dates in Date table and Order table
Create calculated column in Date Table with Day ranges ("30 days or less" etc)
Create measure to identify if an order was placed
Add slicer with date range from Date table and table visual with Customer id.
Add measure to table visual on filter pane and set to "No"
Some of these steps have additional detail below.
2. Create a Date table
We can do this is PowerQuery or in DAX. Here's the DAX version:
Calendar =
VAR
Days = CALENDAR ( DATE ( 2020, 1, 1 ), DATE ( 2020, 12, 31 ) )
RETURN
ADDCOLUMNS (
Days,
"Year Week", YEAR ( [Date] ) & WEEKNUM([Date])
)
Now mark this table as a date table in the "Table Tools" ribbon with the button "Mark as date table"
3. Convert Weeks to dates
For this to work, I have had to create a calculated column in the Order table with the first day of the year first. This can probably be improved upon.
StartYear = DATE(Left(Orders[Year week], 4), 01, 01)
Next the calculated column that we need in the Order table, that identifies the first day of the week. The Variable "DayNoInYear" takes the week number times 7 and substracting 7 to arrive at the first day of the week, returning the nth day of the year. This is then converted to a date with the variable "DateWeek":
Date =
VAR DayNoInYear = RIGHT(Orders[Year week], 2) * 7 - 7
VAR DateWeek = DATEADD(Orders[StartYear].[Date], DayNoInYear, DAY)
RETURN
DateWeek
6. Create calculated column in Date Table with Day ranges
Day ranges =
VAR Today = TODAY()
VAR CheckDate = 'Calendar'[Date] RETURN
SWITCH(TRUE(),
CheckDate - Today <= -90, "90+ days",
CheckDate - Today <= -60 && CheckDate - Today > -90 , "90 days or less",
CheckDate - Today <= -30 && CheckDate - Today > -60 , "60 days or less",
CheckDate - Today <= 0 && CheckDate - Today > -30 , "30 days or less",
"In the future"
)
7. Create measure to identify if an order was placed
Yes - No order =
VAR Yes_No =
IF(
ISBLANK(FIRSTNONBLANK(Orders[Customer id], Orders[Customer id])),
"No",
"Yes"
)
VAR ThirtyDays = SELECTEDVALUE('Calendar'[Day ranges]) = "30 days or less"
VAR SixtyDays = SELECTEDVALUE('Calendar'[Day ranges]) = "30 days or less" || SELECTEDVALUE('Calendar'[Day ranges]) = "60 days or less"
VAR NinetyDays = SELECTEDVALUE('Calendar'[Day ranges]) = "30 days or less" || SELECTEDVALUE('Calendar'[Day ranges]) = "60 days or less" || SELECTEDVALUE('Calendar'[Day ranges]) = "90 days or less"
RETURN
SWITCH(TRUE(),
AND(ThirtyDays = TRUE(), Yes_No = "No"), "No",
AND(SixtyDays = TRUE(), Yes_No = "No"), "No",
AND(NinetyDays = TRUE(), Yes_No = "No"), "No",
Yes_No = "No",
"Yes"
)
Steps 8 and 9
Create slicer with the newly created "Day range" column in the Date table and create a table visual with the "Yes - No order" measure as visual-level filter set at "No" as in screenshot attached below
So, I've 2 tables as under
SALES table:
+----+------------+
| ID | SALE_DATE |
+----+------------+
| 1 | 09-21-2021 |
| 2 | 09-21-2021 |
| 3 | 09-21-2021 |
| 2 | 09-21-2021 |
| 3 | 09-21-2021 |
| 1 | 09-21-2021 |
| 5 | 07-22-2021 |
| 6 | 09-21-2021 |
| 9 | 09-21-2021 |
| 7 | 08-21-2021 |
| 8 | 05-21-2021 |
+----+------------+
CUSTOMER Table
+----+
| ID |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 10 |
+----+
I want to create 2 measures:
1st would be the count of customers that have no sales in the last 2 months, so in this case it would be 2 (8 and 10)
and second measure would give the list of all those customer ID's (8 and 10)
Right now I use this measure to get the list of all ID's that have no sales in last 2 months
show_hide =
VAR current_name = MIN(SALES[ID])
VAR chk_not_in =
IF(
COUNTROWS(
FILTER(
ALL(SALES),
SALES[ID]= current_name && SALES[SALE_DATE])> DATE(YEAR(NOW()),MONTH(NOW())-2, DAY(NOW()))
)
)= 0,
0,
1
)
VAR chk_in =
IF(
COUNTROWS(
FILTER(
ALL(CUSTOMER),
CUSTOMER[ID] = current_name
)
) = 0,
0,
1
)
RETURN IF(chk_in = 1 && chk_not_in = 1, 1, 0)
So every ID with a show_hide of "0" would be the ones that dont have any sales in the last 2 months
I was wondering if there's an easy way to do it and also, I'm not sure how to get the count of all those ID's
First off - I assume your test data was meant to be 2020 instead of 2021 and the ID in the SALES table is a CUSTOMER ID.
I would address this as a measure and a calculated column.
The measure would calculate the customers that have not sold in the past two months. From your data I think you are missing (4) who has not sold anything - bringing the total customers to three (4, 8, 10).
CustomersWithNoSalesIn2Months =
// Work out what date was 2 months ago
VAR twoMonthsAgo = DATE(YEAR(NOW()),MONTH(NOW())-2, DAY(NOW()))
// Count the total distinct customers in the customer table
VAR totalCustomers = CALCULATE(DISTINCTCOUNT(Customer[ID]))
// Count how many distinct customers did have sales in the past 2 months
VAR customersWithSalesInTheLast2Months = CALCULATE(DISTINCTCOUNT(Sales[ID]), Sales[SALE_DATE] > twoMonthsAgo)
// Subtract the customers who did have sales from the total to get the number of customers that did not have sales
RETURN totalCustomers - customersWithSalesInTheLast2Months
The calculated column would be places on the CUSTOMER table and would count how many sales customers had made in the last 2 months.
SalesMadeInTheLast2Months =
VAR MostRecentSale = CALCULATE(MAX(Sales[SALE_DATE]), FILTER(Sales, Customer[ID] = Sales[ID]))
VAR TwoMonthsAgo = DATE(YEAR(NOW()),MONTH(NOW())-2, DAY(NOW()))
RETURN CALCULATE(COUNTROWS(Sales), FILTER(Sales, Sales[SALE_DATE] > TwoMonthsAgo), FILTER(Sales, Sales[ID] = Customer[ID]))
Now you can filter on the Customers table for BLANK sales or use the counts in any other calculation you need. For example, customers 1,2 & 3 have made the most sales in the past 2 months.
I have the following measure:
no_positive_bets =
COUNTAX(
FILTER(
'belgarath match_',
'belgarath match_'[ogion_pnl] >= 0
),
'belgarath match_'[ogion_pnl]
)
Assuming there is a field called 'belgarath match_'[date] how would I group 'belgarath match_'[ogion_pnl] by day so that I can work out the number of positive days?
Edit:
By way of some sample data:
+-----+------------+-----------+
| id_ | date | ogion_pnl |
+-----+------------+-----------+
| 1 | 01/01/2020 | 100 |
| 2 | 02/01/2020 | 100 |
| 3 | 02/01/2020 | -50 |
| 4 | 03/01/2020 | 100 |
| 5 | 03/01/2020 | -150 |
+-----+------------+-----------+
The current snippet I have will return 3 because three rows are positive. However I would like it to return 2 as the first two days are positive.
Try this it may help..
no_positive_bets = COUNTAX(FILTER('belgarath match_', [ogion_pnl] >= 0), [ogion_pnl])
Create this below Custom Column First-
is possitive =
VAR current_date = your_table_name[date]
VAR current_date_sum =
CALCULATE(
SUM(your_table_name[ogion_pnl]),
FILTER(
ALL(your_table_name),
your_table_name[date] = current_date
)
)
RETURN IF(current_date_sum >= 0, 1, 0)
Now create this below Measure-
number of possitive day =
CALCULATE(
DISTINCTCOUNT(your_table_name[date]),
FILTER(
ALL(your_table_name),
your_table_name[is possitive] = 1
)
)
Now add the measure number of possitive day to a card and the output will be as below-
Object: Sum up the nearest date's value by a given date
Here is my data
Table: MyData
+-------------------------------+
| ID TradeDate Value |
+-------------------------------+
| 1 2018/11/30 105 |
| 1 2018/11/8 101 |
| 1 2018/10/31 100 |
| 1 2018/9/30 100 |
| 2 2018/11/30 200 |
| 2 2018/10/31 201 |
| 2 2018/9/30 205 |
| 3 2018/11/30 300 |
| 3 2018/10/31 305 |
| 3 2018/9/30 301 |
+-------------------------------+
I create a table named 'DateList' and use slicer to select a specified date
DateList Slicer
I want to achieve the result as follows:
as of *11/9/2018*
+-----------------------------------+
| ID TradeDate Value |
+-----------------------------------+
| 1 2018/11/8 101 |
| 2 2018/10/31 201 |
| 3 2018/10/31 305 |
+-----------------------------------+
| Total 607 |
+-----------------------------------+
Currently, I try to use the steps to achieve the above result.
First, i want to find the nearest date from table 'MyData' use the new measure
MyMaxDate = CALCULATE(MAX(MyData[TradeDate]),Filter(MyData, MyData[TradeDate] <= FIRSTDATE(DateList[Date]) ))
Second, i create a new measure "MySum" to the sum up the values if [tradedate] equal to the "MyMaxDate"
MySum = CALCULATE(SUM(MyDate[Value]),Filter(MyData, MyData[TradeDate] = MyMaxDate))
Third, i create a matrix to show the result (see Result)
Unfortunately, the result 1313 is different from my goal 607
So, how can i fix my DAX formula to achieve the right result?
Many Thanks
You can calculate the closest date by taking a min over the difference in dates and then taking the minimal date with that minimal difference.
MyDate =
VAR SlicerDate = MIN(DateList[Date])
VAR MinDiff =
MINX(
FILTER(ALL(MyData),
MyData[ID] IN VALUES(MyData[ID])
),
ABS(SlicerDate - MyData[TradeDate]))
RETURN
MINX(
FILTER(ALL(MyData),
MyData[ID] IN VALUES(MyData[ID])
&& ABS(SlicerDate - MyData[TradeDate]) = MinDiff
),
MyData[TradeDate])
From there you can create the summing measure fairly easily:
MySum = CALCULATE(SUM(MyData[Value]), FILTER(MyData, MyData[TradeDate] = [MyDate]))