I'm new to MDX but want to keep my code as clean as possible.
I have a query that looks at todays sales and compares them against LY and LY-1 using the ParallelPeriod function. It looks something like this..
With Member [Date].[SalesCalendar].[DateToday] as [Date].[SalesCalendar].[Date].&[2012-10-26T00:00:00]
SELECT
{[Date].[SalesCalendar].[DateToday],
ParallelPeriod([Date].[SalesCalendar].[Year],1,[Date].[SalesCalendar].[Date].&[2012-10-26T00:00:00]),
ParallelPeriod([Date].[SalesCalendar].[Year],2,[Date].[SalesCalendar].[DateToday]}
*
{[Measures].[Total Sales],[Measures].[Units],[Measures].[Sales Target]}
ON Columns,
[Locations].[Location PK].[Location PK]
on Rows
From MyCube
I start by defining a member that points to today's date. I want to define it once and use it throughout this query (and other queries I write), the theory being I can change it in once place and the underlying query reacts.
The problem I have is that if I try and use this calculated member within the ParallelPeriod function I get no results. With the query above I get results for the first column and the first call to ParallelPeriod (for LY) works but the second call for LY-1, which uses the declared member, fails.
I'm guessing this is down to my lack of knowledge with MDX and so I guess I am missing something fundamental. However, banging my head against the wall isn't working so I need some help!
Any ideas what I am doing wrong?
Thanks
This cannot work because when your query is evaluated [Date].[SalesCalendar].[DateToday] is not replaced with [Date].[SalesCalendar].[Date].&[2012-10-26T00:00:00].
You created a member that will give the same numeric values than [Date].[SalesCalendar].[Date].&[2012-10-26T00:00:00] in the pivot table cells.
You can try this query:
With Member [Date].[SalesCalendar].[DateToday] as [Date].[SalesCalendar].[Date].&[2012-10-26T00:00:00]
Member [Measures].[name] as [Date].[SalesCalendar].CurrentMember.Name
SELECT
{[Measures].[name], [Measures].[Total Sales]} ON Columns,
{[Date].[SalesCalendar].[DateToday], [Date].[SalesCalendar].[Date].&[2012-10-26T00:00:00]} on Rows
From MyCube
The Total Sales will be the same but not [Measures].[name].
Related
I have this table:
and would like to query it to achieve this result:
I'm completely lost how to even start such query. Was looking at UNPIVOT but not even sure how I would apply it here for that purpose.
Any help would be greatly appreciated.
Thanks.
This isn't aggregation. This uses the next/previous value in an ordered result set. You can use the Lead or Lag analytic functions for this.
You can use LEAD(Start) Over (ORDER BY Start) to get Stop, LEAD(Coordinates) Over (ORDER BY Start) to get the previous coordinate and concatenate it to the current one, eg :
SELECT
Start,
LEAD(Coordinates) Over (ORDER BY Start) As STOP,
CONCAT_WS(',',LEAD(Coordinates) Over (ORDER BY Start),Coordinates)
AS COORDINATES
From ThatTable
LEAD will return NULL if there's no value to display, unless a default is specified. This means the last STOP value will be NULL. The question doesn't specify what should be displayed in the last row.
PS
If you provided the actual CREATE TABLE statement and sample data, it would be possible to create a query that actually reproduces what you want. Images can't be copied and queried
Given this table of data:
I'd like to produce this pivot table:
I have an inkling this can be done with the calculated field, and SUMIF, but am not able to get it to work. I think the main blocker is that I'm not able to find good documentation for what I can reference inside of a calculated field formula. My best attempt was =SUMIF(color, "RED")/SUM(), but that produced zeros.
Example table at https://docs.google.com/spreadsheets/d/16htOLbwf47Neo68iFlm9OvFVS_u2Jlc-2thhdUQwrpU/edit?usp=sharing
Any guidance appreciated!
={QUERY(A1:B25,"select A,count(A)/"&COUNT(A:A)&" where B='RED' group by A label count(A)/"&COUNT(A:A)&" 'PCT RED'");{"Grand Total",COUNTIFS(A:A,">=0",B:B,"RED")/COUNT(A:A)}}
Function References
Query
COUNT
COUNTIFS
I think my concern here would be that with a normal pivot table it's robust against data moving around. This seems to break that by referencing specific columns
Method pivot table you must show all color
I think my concern here would be that with a normal pivot table it's robust against data moving around. This seems to break that by referencing specific columns
to "set it free" you can do:
={QUERY({A:B},
"select Col1,count(Col1)/"&COUNT(A:A)&"
where Col2='RED'
group by Col1
label count(Col1)/"&COUNT(A:A)&"'PCT RED'");
{"Grand Total", COUNTIFS(A:A, ">=0", B:B, "RED")/COUNT(A:A)}}
I have this query that it is working as expected for me.
g.V().or(hasLabel("poi"),hasLabel("business")).as("dest")
.outE().inV().hasLabel("region").as("reg")
.select("dest").values("name").as("dest_name")
.select("dest").values("budget").as("dest_budget")
.select("reg").values("name").as("reg_name")
.select("reg_name","dest_name","dest_budget")
This query yields this result.
As I have expected. However I need to retrieve more properties from the "destination" I need to retrieve more 10 properties. This will me into something like this
g.V().or(hasLabel("poi"),hasLabel("business")).as("dest")
.outE().inV().hasLabel("region").as("reg")
.select("dest").values("name").as("dest_name")
.select("dest").values("budget").as("dest_budget")
.select("dest").values("property3").as("property3")
.select("dest").values("property4").as("property4")
//insert more queries like from the above
.select("reg").values("name").as("reg_name")
.select("reg_name","dest_name","dest_budget","property3","property4")
The query will grow long eventually which I am trying to avoid since I need to select values as well from the region as well. So My initial thought was to use select to select multiple values and label them each with an alias like this
g.V().
or(hasLabel("poi"),hasLabel("business"))
.as("destination")
.outE().inV().as("region")
.select("destination").values("name","budget").as("dest_name","dest_budget")
.select("region").values("name").as("reg_name")
.select("dest_name","reg_name","dest_budget")
However I was surprised with this result. Which I was not expecting.
To my understanding the names in values will be mapped to each values passed in the as step. Am I wrong?
Is there anyway for me to retrieve the result from the first screenshot without writing a long query?
The as() labels the step, not the values within that step. So by doing:
.select("destination").values("name","budget").as("dest_name","dest_budget")
you're just naming the values() step twice. I think that you can drastically simplify this traversal for what you want to get as the result though and it doesn't involve stringing together a lot of select() steps:
g.V().or(hasLabel("poi"),hasLabel("business")).
project('dest_name','dest_budget','reg_name').
by('name').
by('budget').
by(out().hasLabel("region").values('name').fold())
You will get a slightly different structure in that "reg_name" will be a list of all the region names rather than having a flattened structure, but you could unroll that I imagine if needed.
I have a to check the current month and current year value for a measure in ssas tabular model but due to type mismatch i am not able to do this. For this, i have created a measure in which i am using this dax query
:CurrMonthYear:=CONCATENATE(CONCATENATE("""",concatenate(year(now()),CONCATENATE(0,month(now())))),"""") output: "201704"
...to calculate currentyear and currentmonth. But when i give this value in a measure like this:
SumOfRevisedForecast:=CALCULATE(SUM(DimRevisedForecast[RevisedForecast]),DimRevisedForecast[Approved] <>"N" && DimRevisedForecast[Approved] <>blank(),'DimRevisedForecast'[CalendarYearMonth] =CurrMonthYear)
...this doesnt work. Though, giving "201704" in place of CurrMonthYear works.
Can anybody help me in this?
Thanks in advance
The problem is not with CurrMonthYear measure, it's with your second formula - CALCULATE function does not accept measures as criteria, only values. That's why it works when you put "201704" explicitly but fails when you use the measure.
A common solution is to wrap the criteria into FILTER function, something like:
CALCULATE(SUM(DimRevisedForecast[RevisedForecast]),
FILTER ('DimRevisedForecast',
'DimRevisedForecast'[CalendarYearMonth] = [CurrMonthYear])
A great explanation of this issue is here:
FILTER() – When, Why, & How to Use It
I have a data set with the following fields:
Serialnumber, Dateofpayment, Paymentamount, Sourcecode and Campaign.
My data has only two campaign types, BS12 and BS13.
I'm trying to plot a running total of the payment amount by Campaign. In the chart I have the following expression for the Y value:
=RunningValue(Fields!PAYMENTAMOUNT.Value, SUM, "Campaign")
However, I keep getting the error:
The Y expression for the chart 'Chart1' has a scope parameter that is not valid for RunningValue, RowNumber of Previous. The scope parameter must be set to a string constant that is equal to the name of a containing group within the Tablix 'Chart1'.
I've looked up multiple sources and I feel like I am doing the right thing, Campaign is clearly a field in my tablix and I have entered it as a string. Why is the error occurring? - Can someone help me?
It's complaining about the scope parameter: "Campaign". Rather than pass the column name to the function, you need to define a group (if there is not one already) on that column in your chart and pass the name of the group instead. An example here.