Nested IF in Google Sheets Issue - if-statement

I have a column of dates, with IF formulas in two other columns. The first IF statement looks for the Max Date in the date list and simply prints TRUE when found.
The second IF statement is meant to print TRUE when the most recent date prior to the Max Date is found.
Originally, I had the following for this:
=IF(B2=WORKDAY(MAX(QQQ!B:B), -1),TRUE,FALSE)
On occasion, however, a day of data will not exist, so the statement must continue beyond Max Date - 1. For this, I tried:
=IF(B2=WORKDAY(MAX(QQQ!B:B), -1),TRUE,IF(B2=WORKDAY(MAX(QQQ!B:B), -2),TRUE,IF(B2=WORKDAY(MAX(QQQ!B:B), -3),TRUE,FALSE)))
The issue with this second approach is TRUE prints for Max Date -2 and Max Date -3, when both exist. I expected that the final condition would be skipped when Max Date - 2 exists, but that's not what occurs.
Any ideas on how this might be better handled are appreciated.

To get the date before the max date, can you use something like:
=query({unique(QQQ!B:B)},"where Col1 is not null order by Col1 desc limit 1 offset 1",0)
Example without offset:
Example with offset:

Related

DAX Doesnt Support Comparison

I have a table of Employees. The Table has both current and budgeted employees. Each employees has a salary, start date and if applicable a term date associated with them.
I am attempting for project the salary spend for each month for the next year based on the following rules:
Hire_Date <= Projected_Month
Term_Date > Projected_Month
Measure:
CALCULATE(sum(INPUT_TECH[Salary_USD]),filter(INPUT_TECH,INPUT_TECH[Hire_Date]<=MEASURE_SWB_SPEND[MEASURE_SWB_SPEND_DATE]&&INPUT_TECH[Term_Date]>MEASURE_SWB_SPEND[MEASURE_SWB_SPEND_DATE]))
The issue here is that if ther is no Term_date, the cell will be blank, and if the cell is blank, the term_date will always be less than the current projected month and therefore the answer will always be 0.
I attempted to fix this by changing the format of the termdate to always either have the number 100,000 formated to read "ACTIVE" or the date they were actually terminated
Format: [=100000]"ACTIVE";[<>100000]mm/dd/yyyy
However, I still receive the following error:
DAX comparison operations do not support comparing values of type Text with values of type Date. Consider using the VALUE or FORMAT function to convert one of the values.
I would suggest not formatting the term date to the string "Active" and then using it for comparisons in your measures. This is why you may be getting that specific error. DAX is finding a data-type issue when making the comparisons. If you want the term to show up as "Active" where term dates are blank for visualization reasons, that may be okay. But I would establish a new calculated column that determines if a term date is blank and replaces it with a date type far into the future so that dates can be compared among dates—something like:
Term Date Blank Fix =
IF(
ISBLANK( INPUT_TECH[Term_Date] ),
// The third argument below could be replaced with a hard year, ex. 2099
DATE( 12, 31, YEAR( TODAY() ) + 10 ),
INPUT_TECH[Term_Date]
)
Then you could use the above column to make your comparisons in your measures. Keep in mind that if you are using a date table with AUTOCALENDAR() that making a date with the year 2099 may wildly expand your date table.

Is there a way to evaluate for first day of the month and last day of the month?

I wanted to know if there is a way to evaluate dates in a manner that evaluate the 1st of the month and the last of a month
I have two columns
Start Date End Date Result
1/1/2020 1/31/2020 Standard
2/5/2020 2/15/2020 Irregular
Goal intended is to use conditional formatting to highlight dates that do not start on the 1st of a month or end on the last of a month
Tried this solution that was suggested out:
IsStandard = [End Date] = Date.EndOfMonth(Date.FromText([End Date])) and [Start Date] = Date.AddDays(Date.AddMonths(Date.EndOfMonth(Date.FromText([Start Date])),-1),1)
Expression evaluates but returns all results as "False" (NOTE: the dates are as text values so I had to add the additional Date.FromText() )
Is there a way to get the expression to evaluate for the date range from 1st month to last month as true and anything else False??
PowerQuery version: You could create a custom column as follows:
IsStandard = [End Date] = Date.EndOfMonth([End Date]) and [Start Date] = Date.AddDays(Date.AddMonths(Date.EndOfMonth([Start Date]),-1),1)
This version returns TRUE if the start date is the start of a month and the end date is the end of a month. To suit your use-case, you could invert the logic or break it up into a column to flag is/is not start of month and another to flag is/is not end of month.
The reason for the weird logic around start of month is PowerQuery provides the Date.EndOfMonth function but not a Date.StartOfMonth, so you have to take the end of a month, subtract a month, then add a day to get to the start of month.
Hopefully this helps :) .
Edit RE: always false...
This is another quirk of the language and not intuitive, but the key is that the type conversion via Date.FromText() returns a nullable date which is different from a strict Date datatype (this can be confirmed by adding a column based on the formula Date.FromText([End Date]) and examining the data type implicitly assigned to the new column, which is not Date.
Probably the simplest option would be to Change Type on the [Start Date] and [End Date] columns to Date prior to evaluating for 'standard' vs 'irregular' and remove the Date.FromText calls from the new column. This might make the fields more consistent anyway but you may run into an issue if any of the raw values are actually null or empty.
If any values are null or empty, you do a check for null to short-circuit the date conversion & check for start/end of month. If the null check fails, you would have the opportunity to then flag the record as something different from 'Standard' and 'Irregular', if that is important for your use-case.

DAX selecting and displaying the max value of all selected records

Problem
I'm trying to calculate and display the maximum value of all selected rows alongside their actual values in a table in Power BI. When I try to do this with the measure MaxSelectedSales = MAXX(ALLSELECTED(FactSales), FactSales[Value]), the maximum value ends up being repeated, like this:
If I add additional dimensions to the output, even more rows appear.
What I want to see is just the selected rows in the fact table, without the blank values. (i.e., only four rows would be displayed for SaleId 1 through 4).
Does anyone know how I can achieve my goal with the data model shown below?
Details
I've configured the following model.
The DimMarket and DimSubMarket tables have two rows each, you can see their names above. The FactSales table looks like this:
SaleId
MarketId
SubMarketId
Value
IsCurrent
1
1
1
100
true
2
2
1
50
true
3
1
2
60
true
4
2
2
140
true
5
1
1
30
false
6
2
2
20
false
7
1
1
90
false
8
2
2
200
false
In the table output, I've filtered FactSales to only include rows where IsCurrent = true by setting a visual level filter.
Your max value (the measure) is a scalar value (a single value only). If you put a scalar value in a table with the other records, the value just get repeated. In general mixing scalar values and records (tables) does not really bring any benefit.
Measures like yours can be better displayed in a KPI or Multi KPI visual (normally with the year, that you get the max value per year).
If you just want to display the max value of selected rows (for example a filter in your table), use this measure:
Max Value = MAX(FactSales[Value])
This way all filter which are applied are considered in the measures calculation.
Here is a sample:
I've found a solution to my problem, but I'm slightly concerned with query performance. Although, on my current dataset, things seem to perform fairly well.
MaxSelectedSales =
MAXX(
FILTER(
SELECTCOLUMNS(
ALLSELECTED(FactSales),
"id", FactSales[SaleId],
"max", MAXX(ALLSELECTED(FactSales), FactSales[Value])
),
[id] = MAX(FactSales[SaleId])
),
[max]
)
If I understand this correctly, for every row in the output, this measure will calculate the maximum value across all selected FactSales rows, set it to a column named max and then filter the table so that only the current FactSales[SaleId] is selected. The performance hit comes from the fact that MAX needs to be executed for every row in the output and a full table scan would be done when that occurs.
Posted on behalf of the question asker

Dax Finding date based on Criteria calculated Column

I couldn't find an answer for my issue elsewhere, so trying my luck here.
I have sales table and my final result should determine if there were sales made for same person in specific period of time, for example within 7 business days. for example:
For ID 123 I have to flag it that sale for products A,B,C where within specified period.
For ID 1234 only sales of products A and B meet the criteria product C was sold in irrelevant time frame.
I've created a date table with indicators that determine for each date if the date is a working day, but i am struggling to calculate the relevant last working day
For example: I need that for 01/01/2019 i will get 10/01/2019 date, based on NUMOFDAYS and FinalWorkday = TRUE, which basically means that i have to count NUMOFDAYS times TRUE statement for each date and return corresponding date.
After that step done I think that it would be pretty easy for me to determine if the sale for a person was made in specific time frame.
If someone can give me direction for that much appreciated
Thank you in advance
You could use a DateTable like this one:
I used the following DAX-expressions for the calculated columns:
nrDays = 7
isWorkDay = WEEKDAY('DateTable'[Date],2) < 6
rankWorkingDays = RANKX ( FILTER ( DateTable, DateTable[isWorkDay] = TRUE () ),
DateTable[Date] , , ASC )
LastWorkDay = LOOKUPVALUE ( DateTable[Date],
DateTable[isWorkDay], TRUE (),
DateTable[rankWorkingDays], DateTable[rankWorkingDays] + DateTable[nrDays])
This issue can be solved by the following, since three are non-working days/holidays we can filter them out via POWERQUERY, than add an Index Column and Another column Which is basically Index column + Number of days wanted, then simply merge duplicate of dates query on Index+number of days wanted column on Index column. And we get the correct date

Sharepoint 2010 Task list: Due date = Start date + 1 month

I want to auto-fill the Due date column by taking the Start date column and add 1 month to it.
This formula I've tried in different ways:
=DATE([Start Date]+[Gap],"dd-mm-yyyy") (I created Gap column as number type and filled it with 28 days; in our language, dd-mm-yyyy seems to be the usual way of showing dates in Sharepoint).
Sharepoint keeps on giving back errors.
Try this formula:
=DATE(YEAR([Start Date]);MONTH([Start Date])+[Gap];DAY([Start Date]))
where [Gap] value is integer.
Note: pay attetion to delimiters - they are locale-dependent, most probably you need to replace , with ;