Ok, so something I could easily solve in Sql server in my case but interesting that this seemed to be tricky
I have a table ComponentPeriod. In it we have the combination of a component (e.g. A,B,C ) and a period (2014 Q1, 2014 Q2, 2014 January etc)
I want the periods to be in descending order (2015 Q4, 2015 Dec, 2015 Nov, 2015 Oct, 2015 Q3 ... etc) and so I need to create a sequential number series to allow this to happen (as we can only order in the client tools by a single column - and so I guess the
technique I'm looking for is used a lot to produce these types of "order by" columns)
I have done this in the past using
Period_Sequence =
calculate
( countrows(Period)
, filter
( Period
, Period[Start_Date] <= earlier(Period[Start_Date])&& Period[Duration_String] = earlier(Period[Duration_String])
)
)
Which was fine when I was referring to a table where Periods where distinct directly but now I
have denormalised this for ComponentPeriod so I need something a little more sophisticated
Whats the best way to get a sequence with perhaps some partitions in across a subset of distinct columns (I guess from SUMMARIZE or similar)
.g. I want
Period_Name, Type, Sequence
2015 Q4, Quarter, 1
2015 Dec, Month, 2
2015 Nov, Month, 3
2015 Oct, Month, 4
2015 Q3, Quarter, 5
2015 Sep, Month, 6
etc
even though there may be multiple records in ComponentPeriod that have the period 2015 Q4, but I want them all to have the value Sequence value of 1?
I've got as far as:
Period_Sequence Desc =
calculate
( countrows(summarize(ComponentPeriod, ComponentPeriod[Period_End_Date]))
, filter
( ComponentPeriod
, ComponentPeriod[Period_End_Date] >= earlier(ComponentPeriod[Period_End_Date])
)
)
But this doesn't distinguish between the different types.
I need an equivalent of the t-sql -
row_number() over (order by Period_End_Date desc, case when 'P3M' then 1 when 'P1M' then 2 end asc
Thanks for any tips!