Converting single result of an MDX set query to a calculated measure - set

I have a SET calculated member that returns me the first invoice date of a specific client ID. This set uses the CurrentSalesPeriod which is also a single result generated from a set.
The problem is that I want to find a workaround so that I'm not forced to use this set on my rows. I would like to make this a measure so I can use it in my columns and for other purposes.
Could anyone help me with that? Is it possible at all in MDX? The query below is working but I would like to use this in another query where I am not allowed to put it on the rows.
SET [First Invoice Date] AS
Iif(
COUNT(
NonEmpty(
[Date invoice].[Invoice date].[Invoice date],
[CurrentSalesPeriod]
* Ytd(StrToMember(#PAR_Date).Parent.Lag(1).LastChild)
* [Point of sale].[Client id].&[46]
* {[Measures].[YTD Sales]}
)
) = 0,
{[Date invoice].[Bonus Calendar - Week].[All].UNKNOWNMEMBER.UNKNOWNMEMBER},
Head(
NonEmpty(
[Date invoice].[Invoice date].[Invoice date],
[CurrentSalesPeriod]
* Ytd(StrToMember(#PAR_Date).Parent.Lag(1).LastChild)
* [Point of sale].[Client id].&[46]
* {[Measures].[YTD Sales]}
), 1
)
)
SET [CurrentSalesPeriod] AS
Tail(
NonEmpty(
[Point of sale].[Management period].[Management period],
{[Measures].[Sales amount]}
* Ytd(StrToMember(#PAR_Date))
* [Point of sale].[Client id].&[46]
), 1
)

Pick a dimension that is not going to be used, and tag on an aggregated member that is the aggregation of your custom set - I don't know your cube but try replacing [AnotherDimension].[AnotherHierarchy]. in the following with a little used Dim.Hier in your cube:
SET [First Invoice Date] AS
Iif(
COUNT(
NonEmpty(
[Date invoice].[Invoice date].[Invoice date],
[CurrentSalesPeriod]
* Ytd(StrToMember(#PAR_Date).Parent.Lag(1).LastChild)
* [Point of sale].[Client id].&[46]
* {[Measures].[YTD Sales]}
)
) = 0,
{[Date invoice].[Bonus Calendar - Week].[All].UNKNOWNMEMBER.UNKNOWNMEMBER},
Head(
NonEmpty(
[Date invoice].[Invoice date].[Invoice date],
[CurrentSalesPeriod]
* Ytd(StrToMember(#PAR_Date).Parent.Lag(1).LastChild)
* [Point of sale].[Client id].&[46]
* {[Measures].[YTD Sales]}
), 1
)
)
SET [CurrentSalesPeriod] AS
Tail(
NonEmpty(
[Point of sale].[Management period].[Management period],
{[Measures].[Sales amount]}
* Ytd(StrToMember(#PAR_Date))
* [Point of sale].[Client id].&[46]
), 1
)
MEMBER [AnotherDimension].[AnotherHierarchy].[Agg_FirstInvoiceDate] AS
Aggregate(
[First Invoice Date]
)
...
...

I am not sure why you want this set as a measure and what it's implication might be, but if you want the date as a measure, then you can simply add one more calculated measure and use the membervalue property to store the value of this date. Adding the definition of the calculated measure only..
MEMBER Measures.FirstInvoiceDate AS
[First Invoice Date].Item(0).MEMBERVALUE
In the final select, just use this new measure. Let me know how it works for you.

Related

Created table in DAX, but can't extract the correct value

I'm attempting to create a measure to give me the rank of a Salesman by Sales. I'll use the measure in a table where one of the columns is Salesman, so there should be the appropriate row context applied. But using
RETURN Calculate( MAXX (RankTable, [Rank] ) )
just gives me a value of 1 for every broker. I can't figure out what to used to just pull out the value for Rank calculated in RankTable. How do I do that?
SumSales = sumx( SalesData, [Sale])
----------
SalesRank =
VAR SummaryTable =
ADDCOLUMNS(
SUMMARIZE( SalesData, [Salesman] ),
"Sales", [SumSales]
)
VAR RankTable =
ADDCOLUMNS(
SummaryTable,
"Rank", RANKX( SummaryTable, [Sales])
)
RETURN
Calculate( MAXX (RankTable, [Rank] ) )
I know that RankTable is correct, since DAX Studio give me this result:
Salesman Sales Rank
A 907 1
B 600 3
C 900 2
D 500 4
Here's code for measure mentioned in comments:
Priced. =
COUNTROWS(
FILTER( 'Cases',
[Date Initiated] >= [MinDate]
&& [Date Initiated] <= [MaxDate]
&& not ISBLANK( [Date To Pricing] )
)
)
I think you might be overcomplicating things, this should work:
SalesRank = RANKX(ALL('Salesdata'[Salesman]), [SumSales])
Reason you're getting 1 for every line is because in a visual the measure is calculated in the context containing a single salesman. So you need to remove this filter using the ALL() function.

exclude specific time for duration calculation

I have this dataset and calculated Datediff1 using below calculated column. Issue is no execution take place between 12am to 5am but when i calculate duration it adds extra 5 hours which is not giving exact duration, Any help will be appreciated.
Datediff1 =
ABS(
DATEDIFF (
CALCULATE (
MIN ( OTC[End Date] ),
FILTER (
OTC,
OTC[Index] = EARLIER ( OTC[Index] )+1
)
),
OTC[End Date],
MINUTE
)
)

How to modify DAX ALLEXCEPT to allow filtering

I have a Table called Tracking, as shown below:
Category
Item Id
Work Date
A
1
1/1/2020
B
1
2/1/2020
C
1
3/1/2020
D
1
4/1/2020
A
2
7/1/2020
B
2
8/1/2020
C
2
9/1/2020
D
2
10/1/2020
Using the below measure, I can get Maximum Work Date from the above Table.
maxWorkDate =
CALCULATE (
MAX ( 'Tracking'[Work Date] ),
ALLEXCEPT (
'Tracking',
'Tracking'[Item Id]
)
)
For Item 1, the maximum work date is 4/1/2020 and for Item 2, the maximum work date is 10/1/2020.
I want to get maximum work date by Excluding Category D. So, For Item 1, the maximum work date should be 3/1/2020 and for Item 2, the maximum work date should be 9/1/2020.
I would like to exclude category D within the measure or I can also exclude it in Visual Filters. Please advise. Thanks
It's possible to implement the measure with a filter expression 'Tracking'[Category] <> "D" like follows
maxWorkDate =
CALCULATE(
MAX( 'Tracking'[Work Date] ),
REMOVEFILTERS( 'Tracking' ),
VALUES( 'Tracking'[Item Id] ),
'Tracking'[Category] <> "D"
)
This is what FILTER is for:
maxWorkDate =
CALCULATE (
MAX ( 'Tracking'[Work Date] ),
FILTER (
ALLEXCEPT (
'Tracking',
'Tracking'[Item Id]
),
'Tracking'[Category] <> "D"
)
)

DAX Year over Year

I have been trying to get this DAX expression to show me cumulative searches for last financial year. Here is an example of the information;
Fiscal Week Fiscal Year Searches Brand
1 14 1000 Example1
1 15 1200 Example1
2 14 1000 Example1
2 15 1200 Example2
My formula below is working a little, but when I apply an slicers to the data it breaks in PowerBI. i.e. if I slice by another field, like brand.
Cum. Searches PY =
IF (
HASONEVALUE ( 'data'[Fiscal Year] ),
CALCULATE (
SUM ( 'data'[Searches] ),
FILTER (
ALL( 'data' ),
'data'[Fiscal Year.] = VALUES ( 'data'[Fiscal Year] ) - 1
&& CONTAINS(
VALUES ( 'data'[Fiscal Week] ),
'data'[Fiscal Week],
'data'[Fiscal Week] )
)
),
BLANK ()
)
I'd appreciate any pointers to where I'm going wrong? Thanks in advance.
I think it can be simplified, I dont understand the need for the HASONEVALUE or CONTAINS functions. I would use something like the following for your measure:
Cum. Searches PY:= CALCULATE(
SUM( Table1[Searches] ),
FILTER(
ALL( Table1[Fiscal Year] ) ,
Table1[Fiscal Year] = MAX( Table1[Fiscal Year] ) - 1
)
)
With that sample data above, this will produce results of:
Example1 2000
Example2 (Blank)
If that's not your expected result, then explain exactly what you want.

How do I convert the Google Maps mysql query to oracle, and is there a performance hit?

I am trying to find businesses within 25 miles of a point, and saw Google's "correct" MySQL query here (http://code.google.com/apis/maps/articles/phpsqlsearch.html#findnearsql) :
SELECT id, (3959 * acos(cos(radians(37)) * cos(radians(lat)) *
cos(radians(lng) - radians(-122)) + sin(radians(37)) * sin(radians(lat))))
AS distance FROM markers HAVING distance < 25 ORDER BY distance LIMIT 0 , 20;
However, my database is in Oracle and I wanted to convert this query to oracle, so here is what I created, which seems to be working as well.
SELECT id, ( 3959 * acos( cos( degree2radian(41.912434) ) * cos( degree2radian( latitude ) ) * cos( degree2radian( longitude ) - degree2radian(-87.6357596) ) + sin( degree2radian(41.912434) ) * sin( degree2radian( latitude ) ) ) ) AS distance
FROM markers
GROUP BY id, latitude, longitude
HAVING ( 3959 * acos( cos( degree2radian(41.912434) ) * cos( degree2radian( latitude ) ) * cos( degree2radian( longitude ) - degree2radian(-87.6357596) ) + sin( degree2radian(41.912434) ) * sin( degree2radian( latitude ) ) ) ) < 25 ;
where, I also defined a function:
create or replace
FUNCTION degree2radian(pin_Degree IN NUMBER)
RETURN NUMBER DETERMINISTIC IS
BEGIN
RETURN pin_Degree / 57.2957795; --1R = 180C
END degree2radian;
It seems like everything is working but I am not sure whether I converted the query correctly, and if so, is there any performance issues, or any ways I can clean this query up (ie. DRY principle etc.)???
Oracle doesn't appear to have a native function for it, so I'd suspect you'd be fine doing it that way.
Cheap solution: Violate normalization and the degrees value in a radians column using a trigger.
Expensive solution: Buy Oracle Spatial, which can answer spatial questions of this nature faster due to the use of r-tree indexing.
I don't know your table structure, but a group by clause with id in it looks strange. Is it actually doing anything? Or could you drop the group by and replace having with where.
In any case oracle will not be able to use an index on that query, which might or might not be a problem.
If it is I can see the following approaches:
- oracle has some special 'spatial' features, there might be something usefull in there for you
- you might add a where close, restricting the result using a simpler outer bound. The idea behind that is, that and index can be used on that constraint and only the smaller result need to get checked against the complex constraint

Resources