MDX iif date greather than doesn't evaluate correctly in my cube - date

I have a problem which seems to be very simple to solve but I can't. In my Fact table I have a Timestamp field which is a smalldatetime Type. This fact is linked to a Time dimension via its fulldate_Fk (also SmallDatetime). So What I would like to have is to compare the timestamp with the FullDate_FK from the fact to create a calculation like this:
iif([Dim Time].[Date].CurrentMember.MemberValue <=
[Fact].[Timestamp].CurrentMember.MemberValue
,[measures].[YTD Actuals]
,[measures].[YTD Actuals]+[measures].[YTD Com])
But it is not working at all. All [Dim Time].[Date] seem to be evaluated as < than the Timestamp.
P.S: The Timestamp is the last date when the data have been loaded in the DB (in my case 31/08)
Here the result I got:
MONTH | YTD Actuals | YTD Com | Calculation;
JAN , 10 , 10 , 10;
FEB , 20 , 10 , 20;
MAR , 40 , 20 , 40;
MAY , 60 , 30 , 60;
JUN , 70 , 50 , 70;
JUL , 85 , 50 , 85;
AUG , 120 , 55 , 120;
SEP , 120 , 60 , 120;
OCT , 120 , 70 , 120;
NOV , 120 , 80 , 120;
DEC , 120 , 90 , 120;
From August, I should have the sum of Actuals YTD and Com YTD in the calculation, but I still have the Actuals YTD only?
Extra Info
I'm using PivotTable just by dragging attributes in Excel. Month in rows and measures (the 2 YTD and the new calculated member)

If you build a new calc which is:
[Fact].[Timestamp].CurrentMember.MemberValue
What does it return when you add it to your PivotTable? Null? I suspect the CurrentMember is the All member so MemberValue is null. But let's test that.
Do all rows in the fact table have the same Timestamp or are there many different timestamps?
If your fact table has 10000 rows are you expecting the IIf calc will be evaluated 10000 times (once for each row)? That's not the way MDX works. In your PivotTable that has 12 rows the IIf calc gets evaluated 12 times at the month grain.
If you want the calculation to happen on each of the 10000 rows then write the calculation in SQL and do it in a SQL view before it gets to the cube.
To make the calc work as you intend in the cube consider doing the following. Add a new column in your DimTime SQL table called Today Flag. It should be updated during the ETL to be Y only on the row which is today and should be N on other rows. Then add that column as a new attribute to your Dim Time dimension. You can make it Visible=False. Then go to the Calculations tab and flip to the Script view and replace your current [Measures].[Calculation] calc with this:
Create Member CurrentCube.[Measures].[Calculation] as
[measures].[YTD Actuals];
Scope({Exists([Dim Time].[Month].[Month].Members,[Dim Time].[Today Flag].&[Y]).Item(0).Item(0):null});
[Measures].[Calculation] = [measures].[YTD Actuals]+[measures].[YTD Com];
End Scope;

Related

Number of days in a month in DB2

Is there a way to find the number of days in a month in DB2. For example I have a datetime field which I display as Jan-2020, Feb-2020 and so on. Based on this field I need to fetch the number of days for that month. The output should be something like below table,
I'm using the below query
select reportdate, TO_CHAR(reportdate, 'Mon-YYYY') as textmonth from mytable
Expected output
ReportDate textMonth No of Days
1-1-2020 08:00 Jan-2020 31
1-2-2020 09:00 Feb-2020 29
12-03-2020 07:00 Mar-2020 31
Try this:
/*
WITH MYTABLE (reportdate) AS
(
VALUES
TIMESTAMP('2020-01-01 08:00:00')
, TIMESTAMP('2020-02-01 09:00:00')
, TIMESTAMP('2020-03-12 07:00:00')
)
*/
SELECT reportdate, textMonth, DAYS(D + 1 MONTH) - DAYS(D) AS NO_OF_DAYS
FROM
(
SELECT
reportdate, TO_CHAR(reportdate, 'Mon-YYYY') textMonth
, DATE(TO_DATE('01-' || TO_CHAR(reportdate, 'Mon-YYYY'), 'dd-Mon-yyyy')) D
FROM MYTABLE
);
Db2 has the function DAYS_TO_END_OF_MONTH and several others which you could use. Based on your month input, construct the first day of the month. This should be something like 2020-01-01 for Jan-2020 or 2020-02-01 for Feb-2020. Follow the link for several other conversion functions which allow you to transform between formats and to perform date arithmetics.
convert your column to a proper date and try this: day(last_day(date_column))

How to create calculated field based on another time series

I have a set of prices as data source for given timeseries and I would like to create a calculated field by combining two prices for each date: i.e.,
Price A *5 - Price B.
Data source:
Date Product Price
01.01.2018 A 10
01.01.2018 B 15
02.01.2018 A 20
02.01.2018 B 30
03.01.2018 A 10
03.01.2018 B 30
I don't know how to write the formula correctly for the Calculated field.
What I expect is to build the following table:
Date A B Combined Price (A *5 - B)
01.01.2018 10 15 35
02.01.2018 20 30 70
03.01.2018 10 30 20
Thank you
Answer from Mohfooj can be found in Tableau forum here: https://community.tableau.com/message/900181#900181

Power BI Rolling Average DAX to plot correctly on Column Chart

I have a problem with the measure of the 3mth rolling average to visualise it correctly on the graph.
The data model is here:
https://docs.google.com/spreadsheets/d/1naChcuZtjSbk0pVEi1xKuTZhSY7Rpabc0OCbmxowQME/edit?usp=sharing
I am using the formula below to calculate 3mth average through a measure:
Product3Mth = CALCULATE(SUM('Table'[Product A uncum]);DATESINPERIOD('Table'[Date];LASTDATE('Table'[Date]);-3;MONTH))/3
When I am plotting it as a table it is showing right values for each month.
But When I am plotting it in the column chart together with Product A Accumulated I am getting wrong value which is the value for Product unaccum /3 insted of sum of 3 consecutive values for Product unaccum /3.
What should I change in the DAX to have it visualised correctly? Please HELP
It seems you've stumble accross quite a few Power BI "gotchas" here when it comes to both the format of the date in your source data and the way you've chosen to display the Date column in your visulaization. But I think I've figured it out. This is my result:
And just to verify some numbers:
(4043 + 20 + 158) / 3 = 1469
(189+ 200 + 207) / 3 = 199
And here are the details:
I used this dataset where I've changed the names slightly to make it easier to write DAX expressions and imported it using Get Data
Date unAcc ACc
01-10-2017 00:00 4043 4043
01-11-2017 00:00 205 4248
01-12-2017 00:00 158 4406
01-01-2018 00:00 142 4548
01-02-2018 00:00 312 4860
01-03-2018 00:00 258 5118
01-04-2018 00:00 176 5294
01-05-2018 00:00 210 5504
01-06-2018 00:00 189 5693
01-07-2018 00:00 200 5893
01-08-2018 00:00 207 6100
And for reasons still uknown to me, I had the same issues as you had with the Date column. But following some tips from the Power BI community, I created a Date2 like this :
Date2 =
DATE('Table1'[Date].[Year];'Table1'[Date].[MonthNo];1)
Then I calculated the three month average using a
Moving_Average_3_Months =
CALCULATE (
AVERAGEX ( 'Table1'; 'Table1'[unAcc] );
DATESINPERIOD (
'Table1'[Date2];
LASTDATE ( 'Table1'[Date2]);
-3;
MONTH
)
)
Now, if you insert a column chart and assign Date2 to the Axis and Moving_Average_3_months together with unAcc to Values, you'll get this:
And that's not what we want. So go to the Visualization settings and change Date2 from Date Hierarchy to simply Date2 like this:
And that's it:
And here's the whole thing as a table so you can see that the numbers are correct:
In your case, maybe the only thing you have to do is that very last part.
Please don't hesitate to let me know how it works out for you!
I ask in the last comment the issue about the calculated value (as column or measure) because I have different results based on this, as you can see in the example below:
The Product3Mth is based on the calculated column and the Product3Mth 2 is based on the measure (result you want!).
Hope this example can help you. If not tell me please!
The DATESINPERIOD and LASTDATE functions will not work correctly, because 'Table'[Date] is not a continous range of dates. These functions need to be based on a datetable.
Try something like this
Product3MthAVG =
VAR rowDate = 'Table'[Date]
RETURN
CALCULATE (
SUM ( 'Table'[Product A unaccum] );
FILTER (
'Table';
'Table'[Date]
< DATE ( YEAR ( rowDate ); MONTH ( rowDate ) + 1; DAY ( rowDate ) )
&& 'Table'[Date]
>= DATE ( YEAR ( rowDate ); MONTH ( rowDate ) - 2; DAY ( rowDate ) )
)
)
/ 3

Qlikview - Data between dates; filter out data past or future data depending on selected date

I've seen threads where the document has Start Date and End Date "widgets" where users type in their dates, however, I'm looking for a dynamic solution, for example on the table below, when I select a date, say "1/1/2004", I only want to see active players (this would exclude Michael Jordan only).
Jersey# Name RookieYr RetirementYr Average PPG
23 Michael Jordan 1/1/1984 1/1/2003 24
33 Scotty Pippen 1/1/1987 1/1/2008 15
1 Derrick Rose 1/1/2008 1/1/9999 16
25 Vince Carter 1/1/1998 1/1/9999 18
The most flexible way is to IntervalMatch the RookieYr * RetireYr dates into a table of all dates. See http://qlikviewcookbook.com/recipes/download-info/count-days-in-a-transaction-using-intervalmatch/ for a complete example.
Here's the interval match for your data. You'll can obviously create your calendar however you want.
STATS:
load * inline [
Jersey#, Name, RookieYr, RetirementYr, Average, PPG
23, Michael Jordan, 1/1/1984, 1/1/2003, 24
33, Scotty Pippen, 1/1/1987, 1/1/2008, 15
1, Derrick Rose, 1/1/2008, 1/1/9999, 16
25, Vince Carter, 1/1/1998, 1/1/9999, 18
];
let zDateMin=37000;
let zDateMax=40000;
DATES:
LOAD
Date($(zDateMin) + IterNo() - 1) as [DATE],
year( Date($(zDateMin) + IterNo() - 1)) as YEAR,
month( Date($(zDateMin) + IterNo() - 1)) as MONTH
AUTOGENERATE 1
WHILE $(zDateMin)+IterNo()-1<= $(zDateMax);
INTERVAL:
IntervalMatch (DATE) load RookieYr, RetirementYr resident STATS;
left join (DATES) load * resident INTERVAL; drop table INTERVAL;
There's not much to it you need to load 2 tables one with the start and end dates and one with the calendar dates then you interval match the date field to the start and end field and from there it will work the last join is just to tidy up a bit.
The result of all of that is this ctrl-t. Don't worry about the Syn key it is required to maintain the interval matching.
Then you can have something like this.
Derrick Rose is also excluded since he had not started by 1/1/2004

Find the end date of a quarter given a date

I have the end dates of each quarter as the PK for a table, and I need to compare a date to see which quarter-ending value would be used in a calculation.
The Table looks like:
EndingDate Value
12/31/2012 $1,000
For example, given 3/1/2013 I would need to return 12/31/2012 and use that date to retrieve the $1,000 value.
Does anyone know what to use in MS Access 2007 to perform this? I tried:
DATEADD(dd, -1, DATEADD(qq, DATEDIFF(qq, 0, DATEINQUESTION), 0))
The calculation always uses the previous ending quarter's date, and the value associated with that date.
One way to do it would be with a combination of DMax() and DLookup(). For sample data in a table named [EndingBalances]
EndingDate Value
---------- -----
2012-09-30 900
2012-12-31 1000
2013-03-31 1100
2013-06-30 1200
2013-09-30 1300
2013-12-31 1400
the expression
DMax("EndingDate","EndingBalances","EndingDate<#2013-03-01#")
would return the date
2012-12-31
and therefore the expression
DLookup("Value","EndingBalances","EndingDate=#" & Format(DMax("EndingDate","EndingBalances","EndingDate<#2013-03-01#"), "yyyy-mm-dd") & "#")
would return the value
1000
I used this solution and it worked for me:
Format(DateAdd("s",-1,DateAdd("q",DateDiff("q","1/1/1950",Date()),"1/1/1900")),"Short Date")
It works so far..