powerpivot dates older than today - powerpivot

I am trying to create a calculated colum which basically states that if the [CloseDate] is blank and [LastModifiedDate] is older than 4 days return a numerical value.
I've started with =ISBLANK([CloseDate]) but can't seem to get the rest of the statmennt to work
I'm new to powerpivot so any help would be helpful

MyColumn =
IF(
ISBLANK('table'[CloseDate]
&& TODAY() - 'table'[LastModifiedDate] > 4
,<your number to return>
,<else condition>
)

Related

How to add +1 in a specific column if date in another column = '2020-11-10' for example in SQL

I'm working on a report that shows if a certain shipper performs as agreed - if they promised 1-day-delivery, it should be 1 day and not 2.
This is a screenshot of the table i created:
table
Column 'transitdays_int' shows the difference between column 'shippingdate_date' and 'deliverydate_date' in days
Column 'transit_treshold shows the number of days the shipper promised it would take to get it delivered
In column 'success_ratio' i converted these values with a CASE-statement to 'on time' or 'late delivery'
Issue i have now is that i want to add +1 to column 'transit_treshold' if column 'shippingdate_date' = '2020-11-10'. Is that even possible? I read about many different solutions, but none of them seems to be exactly what i'm looking for...
Thanks in advance!
It sounds like you just need a basic update:
UPDATE yourTable
SET transit_treshold = transit_treshold + 1
WHERE shippingdate_date = '2020-11-10'
If this is a report, you are just looking for a query and not to actually update the database values, correct?
If so, I would just update your query as:
select
case when cast(shippingdate_date as date) = cast('2020-11-10' as date)
then transit_treshold + 1
else transit_treshold
end as [Transit Days]
If you could provide your actual query it might be easier to help.
Also, what is special about 11/10/2020? Are you looking for this date to be hard coded or do you need it to be dynamic?
Update TableName
set transit_treshold=transit_treshold+1
where shippingdate_date = '20201110'

Calculate measure using [field1] - [field2] > x as filter

I have a fact table containing columns OriginalPrice and PaidPrice for customer transactions. I would like to know how many transactions befenit from a discount OriginalPrice - PaidPrice between 10 and 20 dollars. I already have a measure #Customer that describes the number of customers.
This is for a PowerBI report using a Live connection to SSAS. New columns and some DAX functions are not available in this connection mode.
DiscountTier1 = CALCULATE([#Customer],(FactTable[OriginalPrice]-FactTable[PaidPrice]) >= 10, FactTable[OriginalPrice]-FactTable[PaidPrice]) < 20)
By doing this I want to know the number of customers that had a discount between 10 and 20 dollars.
Currently I have an error as follows:
CALCULATE has been used in a True/False expression that is used as a table filter expression. This is not allowed
Any suggestions of how to achieve this or what I am doing wrong?
Thank you!
Add the FILTER function as the second parameter of CALCULATE, and in this you can filter the fact table for records satisfying your criteria. I'd also recommend using the AND function for better readability and long term maintenance.
DiscountTier1 =
CALCULATE (
[#Customer],
FILTER (
FactTable,
AND (
FactTable[OriginalPrice] - FactTable[PaidPrice]
>= 10,
FactTable[OriginalPrice] - FactTable[PaidPrice]
<= 20
)
)
)
You are searching for a measure like this:
DiscountTier1 =
COUNTROWS(
FILTER(
SUMMARIZE (
'FactTable';
'FactTable'[customer_id];
"DISCOUNT";
CALCULATE(SUM(FactTable[OriginalPrice])) - CALCULATE(SUM(FactTable[PaidPrice]))
);
[DISCOUNT] <= 20 && [DISCOUNT] >= 10
)
)
This query calculates the discount of all rows, and filter the rows with a discount between 10 and 20

PowerPivot return value of last date for each Uniquw

Good day everyone,
So I've been struggling with this calculation in PowerPivot, where I want the formula to return the last project status for each project, from the last date. I have used a few formulas to at least get the last date for each project number, but have not managed to get that right either. I've tried variants of LASTNONBLANK, MAX, LASTDATE etc. but these all miss the crucial bit where it does it for each project number.
=CALCULATE(MAX(GetTimeSheetData[Date]),ALL(GetTimeSheetData))
=LASTNONBLANK(GetTimeSheetData[Date],GetTimeSheetData[Code])
.....and many others.
I have the file on the link below that shows what I need to do, any help will be most welcome......I'm busy going crazy!!
https://drive.google.com/file/d/0B29yA0i2Te9ycFVSclBuZUVzSUU/view?usp=sharing
Thanks!!
Llewellyn
Here is the code for the max date:
=CALCULATE( MAXA( GetTimeSheetData[Date]), ALLEXCEPT(GetTimeSheetData,GetTimeSheetData[Code]))
This code would get you the last status in a calculated column:
=CALCULATE(
VALUES ( GetTimeSheetData[Project Status]),
FILTER( ALLEXCEPT(GetTimeSheetData, GetTimeSheetData[Code], GetTimeSheetData[CalculatedColumn1]),
GetTimeSheetData[Code] = EARLIER( GetTimeSheetData[Code]) &&
GetTimeSheetData[Date] = CALCULATE( MAX( GetTimeSheetData[Date]),
ALLEXCEPT(GetTimeSheetData, GetTimeSheetData[Code], GetTimeSheetData[CalculatedColumn1]),
GetTimeSheetData[Code] = EARLIER( GetTimeSheetData[Code]))))
This code would get you a calculated field rather than utilizing a column:
Current Status:=CALCULATE(
VALUES ( GetTimeSheetData[Project Status]),
FILTER( ALLEXCEPT(GetTimeSheetData, GetTimeSheetData[Code]),
GetTimeSheetData[Date] = CALCULATE( MAX( GetTimeSheetData[Date]),
ALLEXCEPT(GetTimeSheetData, GetTimeSheetData[Code]))))
Either way, for the last example of taking the last status, you still need a manner of identifying which one was the true last. In this case, I would suggest either a) a timestamp rather than a date or b) another identifying field.

Counting items with multiple criteria

I have a table (getECRs) in PowerPivot.
Right now, I've been able to create a calculated column that counts how many times the row's customer ID (BAN) occurs in the BAN column with the following formula:
=CALCULATE(COUNTROWS(getECRs),ALLEXCEPT(getECRs,getECRs[BAN]))
What I'm having difficulty with is adding multiple criteria to the CALCULATE formula in PowerPivot.
Each row has a column that gives the date the request was generated _CreateDateKey. I'm trying to include criteria that would only include multiple BANs if they fall within 7 days (before or after) the _CreateDateKey for the row.
For example for one BAN, there are the following dates and their expected counts:
_CreateDateKey Count Explanation
6/13/2014 3 Does not include 6/23
6/13/2014 3 Does not include 6/23
6/16/2014 4 Includes all
6/23/2014 2 Does not include the 2 items from 6/13
In Excel I would use a COUNTIFS statement, like below to get the desired result (using table structure naming)
=COUNTIFS([BAN],[#BAN],[_CreateDateKey],">="&[#[_CreateDateKey]]-7,[_CreateDateKey],"<="&[#[_CreateDateKey]]+7)
But I can't seem to figure out the relative criteria needed for the dates. I tried the following as a criteria to the CALCULATE function, but it resulted in an error:
getECRs[_CreateDateKey]>=[_CreateDateKey]-7
Error: Column '_CreateDateKey' cannot be found or may not be used in this expression.
This formula answers your specific question. It's a good pattern to get down as it's highly re-usable - the EARLIER() is referencing the value of the current row (slightly more complex than this but that is the end result):
=
CALCULATE (
COUNTROWS ( getECRs ),
FILTER (
getECRs,
getECRs[BAN] = EARLIER ( getECRs[BAN] )
&& getECRs[_CreateDateKey]
>= EARLIER ( getECRs[_CreateDateKey] ) - 7
&& getECRs[_CreateDateKey]
<= EARLIER ( getECRs[_CreateDateKey] ) + 7
)
)
Fundamentally you should probably be looking to get away from the 'Excel mindset' of using a calculated column and deal with this using a measure.
An adaptation of the above would look like this - it would use the filter context of the PIVOT in which you were using it (e.g. if BAN was rows then you would get the count for that BAN).
You may need to adjust the ALL() if is too 'open' for your real world context and you might have to deal with totals using HASONEVALUE():
=
CALCULATE (
COUNTROWS ( getECRs ),
FILTER (
ALL(getECRs),
getECRs[_CreateDateKey] >= MAX ( getECRs[_CreateDateKey] ) - 7 &&
getECRs[_CreateDateKey] <= MAX ( getECRs[_CreateDateKey] ) + 7
)
)

Rails / SQL : Group by and sum each day in the last 2 weeks

I'm trying to sum each day in the last two weeks.
This code:
Invoice.group('date(filled_at)').sum(:lines_price).to_a
returns:
[["2012-12-15", #<BigDecimal:ac40068,'0.19275E4',18(45)>], ["2012-12-17", #<BigDecimal:a759234,'0.764235E3',18(45)>]]
Which is correct but I need it the return the values in the last two weeks even if the value is 0.0.
So the result should be:
[["2012-12-03", 0.0], ... , ["2012-12-17", #<BigDecimal:a759234,'0.764235E3',18(45)>]]
Thank you.
UPDATE
having("date(filled_at) > ?", Date.today - 14)
solves the last 14 days question. But I still don't know how to show 0.0 values.
You're going to have a hard time doing this in SQL because you are trying to force the db to return values that don't exist. It is much easier to do this in ruby. Here is a succinct way:
summary = Invoice.group('date(filled_at)').sum(:lines_price).to_a
past_two_weeks = ((Date.today - 14).. Date.today)
final_result =Hash[*past_two_weeks.map(&:to_s).product([0.0]).flatten].merge Hash[*summary.flatten]
final_result.to_a