PowerPivot DAX formula if then for dates - excel

I have a column named ExpireDate in my PowerPivot table (not PowerBI) and want to make another column based on the value of Today's Date - ExpireDate with a DAX formula like below:
=if(Format([ExpireDate] -today(), "General Number") <= 90, "Less than 3 months",
if(90 < Format( [ExpireDate] -today() ,"General Number") <= 180, " 3 to 6 months",
if([Format (ExpireDate]-today() ,"General Number") > 180, "More than 6 months","Other"))
But this formula keeps showing error messages, saying it needs a proper formula. Anybody knows how to deal with this problem? Thanks a lot.

Assuming you are using Excel 2016, and your table name is "Table":
=
VAR Expiration = Table[ExpireDate] - TODAY ()
RETURN
SWITCH (
TRUE (),
Expiration <= 90, "Less than 3 months",
Expiration <= 180, " 3 to 6 months",
Expiration > 180, "More than 6 months",
"Other"
)

Related

EXCEL: How to find out is a cells date is "This Week", "Last Week" or the "Previous Week"

I have a table that has various dates in Column A ("Date Worked")
Column B ("Week Num") Lists those dates as the week number
Column C ("Week") should show if the date is for This Week, Last Week, Previous Week, Or Older or in the future.
I have tried the following formula, but it just shows everything as "Old"
=IF( (WEEKNUM(TODAY()) - WEEKNUM([#[Week Number]])) < 0,
"Future",
IF( (WEEKNUM(TODAY()) - WEEKNUM([#[Week Number]])) = 0,
"This Week",
IF( (WEEKNUM(TODAY()) - WEEKNUM([#[Week Number]])) = 1,
"Last Week",
IF( (WEEKNUM(TODAY()) - WEEKNUM([#[Week Number]])) = 2,
"Previous Week",
"Old"
)
)
)
)
I have also tried WEEKNUM(Now()) instead, but it made no difference.
What am I doing wrong?
Thanks in advance
DD
You don't need to wrap the Week Number column in the WEEKNUM function.
=IF(
WEEKNUM(TODAY()) - [#[Week Number]] < 0,
"Future",
IF(
WEEKNUM(TODAY()) - [#[Week Number]] = 0,
"This Week",
IF(
WEEKNUM(TODAY()) - [#[Week Number]] = 1,
"Last Week",
IF(
WEEKNUM(TODAY()) - [#[Week Number]] = 2,
"Previous Week",
"Old"
)
)
)
)
If you are using Excel 2019 or newer you can use the IFS function to make this easier to read:
=IFS(WEEKNUM(TODAY())-[#[Week Number]]<0,"Future",WEEKNUM(TODAY())-[#[Week Number]]=0,"This Week",WEEKNUM(TODAY())-[#[Week Number]]=1,"Last Week",WEEKNUM(TODAY())-[#[Week Number]]=2,"Previous Week",TRUE(),"Old")
If you are using Excel 365 or newer you can use the LET function to create a variable for the current week number, and then check that variable to assign the status. The SWITCH function performs much like IFS:
=LET(weekNumber,WEEKNUM(TODAY())-[#[Week Number]],SWITCH(TRUE(),weekNumber<0,"Future",weekNumber=0,"This Week",weekNumber=1,"Last Week",weekNumber=2,"Previous Week","Old"))
When working with standard IF functions it's easier if you first make a list a of how many options you want, because you'll need as many ifs minus 1 as options. So your options are:
Older Week
Last Week
This Week
Next Week
Future Week
These are 5 options so you'll need 4 IFS. My formula is:
=IF(B1=WEEKNUM(TODAY())-1;"Last Week";IF(B1=WEEKNUM(TODAY())+1;"Next week";IF(B1<WEEKNUM(TODAY());"Older week";IF(B1>WEEKNUM(TODAY());"Future week";"This Week"))))
Using weeknum might be critical if you have dates for different years.
If you are using Excel 365 I would suggest using a LET-formula:
=LET(
mondayDate, [#Date]-WEEKDAY([#Date],2),
mondayToday,TODAY()-WEEKDAY(TODAY(),2),
diffWeeks,(mondayToday - mondayDate)/7,
diffWeeks2,IF(diffWeeks<0,1,IF(diffWeeks>3,2,diffWeeks +3)),
CHOOSE(diffWeeks2,"Future","old","this week","last week","previous week"))
First two steps retrieve mondays of the original date and today.
Subtracting and dividing by 7 results in the diff of weeks.
To be able to use the choose function, diffWeeks2 returns
1 for future dates
2 for dates older than 3 weeks
for all other dates 3 is added, so that the correct wording can be retrieved
CHOOSE then returns the correct wording.

How to use Calculate with a Measure that originally used VAR to get the result

I am fairly new to DAX, but the end result I want to achieve is to calculate how many of below cancelations per bucket have been cancelled / total sales (Total sales is a measure I can quote in DAX - already created)
In other words I'd like to see VARs/Buckets divided as so
"Cancelled < 30 days" / [# Total sales]
"Cancelled 31-60 Days" / [#Total sales]
and so on.
Trying to create separate measure for each of the buckets like so:
0-30 Bucket =
CALCULATE (
[Cancellation buckets],
[Cancellation buckets] = "Cancelled <30 Days"
)
but getting an error message -> A function CALCULATE has been used in a True/False expression that is used as a table filter expression. This is not allowed.
Below is a bucketing measure that #RADO kindly helped with
Cancellation buckets =
VAR Cancellations = [# Cancellations]
RETURN
SWITCH (
TRUE (),
ISBLANK([# Cancellations]), BLANK(),
Cancellations <= 30, "Cancelled <30 Days",
Cancellations <- 60, "Cancelled 31-60 Days",
Cancellations <= 90, "Cancelled 61-90 Days",
Cancellations > 90, "Cancelled 90+"
)
I want to calculate that to then be able to divide the sum of these calc per total sales
Any help is muchly appreciated!

Getting Excel to select from one of three range options

I'm trying to devise a dynamic spread sheet involving pension contribution percentages which increases with the age of the employees.
I've used the =now() function to calculate their ages on a dynamic basis and I now need to get excel to look at their age in cell H2 and apply the following criteria, altering automatically as their age increases into the next bracket:
if they are currently aged between 18 - 39 pension contribution is 6%
between 40 - 49 it is 7%
and over 50 it is 10%
The formula I've devised is picking up the correct percentages for those 39 & under and for those 50+ but I can't get it to recognize the 7% for those between 40 - 49.
Can anybody tell me where I'm going wrong?
=IF(H2>=OR18<=39,"6%",IF(H2>=OR40<=49,"7%",IF(H2>=OR50>100,"10%")))
Hi and welcome to Stack Overflow!
So assuming the value to test is in cell A1, the basic formula for matching within a range (say 1-10) is:
=IF(AND(A1 >= 1, A1 <= 10), "In Range", "Out of Range")
So expanding this to 3 ranges (18-39, 40-49, and 50+), and substituting your percentages, we get:
=IF(AND(A1 >= 18, A1 <= 39), "6%", IF(AND(A1 >= 40, A1 <= 49), "7%", IF(A1 >= 50, "10%")))
So a loose end that needs tying up is what to do if the age is less than 18 - currently this formula will produce FALSE. You might want to put something else in by adding a result for the second condition in the last test - so where the value doesn't match >= 50, e.g.
... IF(A1 >= 50, "10%", "NOT APPLICABLE") ...
or some other value that's appropriate.

Using IF AND and multiple conditions

I need to create a formula to identify the tier based on the following conditions:
Result Conditon1 Condition2
tier_1 >=61 >=300001
tier_2 >=25 - <=60 >=100001 - <=300000
tier_3 <=24 >=0 - <=100000
I have created individual formulas but I can't put them together
=if(and(B19>=61,B1823>=300001),"Tier 1")
=IF(and(B18>=100001,B18<=300000,B19>=25,B19<=60),"Tier 2")
=if(and(B19>=0,B19<=100000,B18<=24),"Tier 3")
Your help is greatly appreciated.
Thank you,
Carlos
The usage of the Excel IF function is:
=IF(some_condition, "true", "false")
You may nest your current IF calls to get the desired logic:
=IF(AND(B19 >= 61, B1823 >= 300001), "Tier 1",
IF(AND(B18 >= 100001, B18 < =300000, B19 >= 25, B19 <= 60), "Tier 2",
IF(AND(B19 >= 0, B19 <= 100000, B18 <= 24), "Tier 3", "No Tier")))
Note that in the innermost/final IF call, I provided an else value of No Tier. This is because there has to be an else value. If you instead want everything which does not match tiers 1 and 2 to default to tier 3, then you can remove this final IF and just provide Tier 3 as the else value.

Combining values in a Notes/Domino column that is categorized

I have a notes view that shows hundreds of peoples’ ages. I then created a categorized view so I can see how many people are in each age. This works fine.
Problem is, I want to group these ages into five year brackets (I.e. age 0 to 5, 6 to 10,11 to 15,etc)
The field is called ‘age’ and it is a text field.
Is there an easy way to do this?
Thanks in advance.
A simple, brute-force way is to create a formula for your column that is categorized.
#If(Age <= 5; "0 to 5";
Age > 5 & Age <= 10; "6 to 10";
Age > 10 & Age <= 15; "11 to 15";
....
Age > 100; "Over 100"; "Not specified");
Also, I believe you could create a hidden Age column before this one and sort by it to make the categories appear in age order

Resources