I am stuck on modelling a tiny example in Microsoft SQL Server Analysis Services. I do consider myself advanced in this technology, but this problem I cannot get my head around.
It comes down to a measure group which is restricted by three dimensions. Two dimensions behave exactly as expected, limiting the rows delivered back.
The third dimension does not have any effect on this measure group. For each entry on this third dimension, the same value (the total) is delivered as measure. The only noticeable difference between the dimensions is, the ones that do work link through their key element, while the one that does not does link through a non key attribute.
The item names are in German language, hope the following translation helps
Benutzer -> User
Kunde -> Customer
Bestellung -> Order
I hope the following screen shots help to show the relevant modelling details:
Dim Bestellung (the one that is not working)
Dimension Usage in Cube
MDX Query that is working
If I do query using the Kunde ("customer") dimension which is linked on key, everything behaves as expected. For the customers with 1s and 3s in their "name", there is a result, and null for the others.
SELECT {
[Measures].[Anzahl SecKunde]
} ON COLUMNS
, {
[DIM Kunde].[Hierarchie Kunde].[Kunde]
} ON ROWS
FROM
[BiEvaluation]
WHERE
[DIM Benutzer].[Hierarchie Benutzer].[Benutzer].&[Domain\User1]
MDX Query not working
If I however query by Bestellung ("Order"), I get the total of 2 for each order. The orders B1, B2, B5 and B6 are from customers 1 and 3, so I'd like to see a count of 1 there!
SELECT {
[Measures].[Anzahl SecKunde]
} ON COLUMNS
, {
[DIM Bestellung].[Hierarchie Bestellung].[Bestellung].AllMembers
} ON ROWS
FROM
[BiEvaluation]
WHERE
[DIM Benutzer].[Hierarchie Benutzer].[Benutzer].&[Domain\User1]
MDX Query not working "fixed"
I found a way to make it work, yet... I do not understand why. The "trick" is to include the non-key-attribute the dimension-fact link is on. This gives the result I consider correct, a 1 for B1, B2, B5 and B6 (because those are done by customers 1 and 3)
SELECT {
[Measures].[Anzahl SecKunde]
} ON COLUMNS
, {
[DIM Bestellung].[Hierarchie Bestellung].[Bestellung].AllMembers
* [DIM Bestellung].[ID Kunde].[ID Kunde].AllMembers -- Only change needed
} ON ROWS
FROM
[BiEvaluation]
WHERE
[DIM Benutzer].[Hierarchie Benutzer].[Benutzer].&[Domain\User1]
Sorry for this awful long post. If anybody can share any idea, why this is happening I'd appreciate it a lot.
I could most likely work with including the attribute, but this is a huge source for problems and errors. I do not understand why having it there makes this difference.
I think it is not relevant to the question, but I'll include the versions of the tools anyway:
Visual Studio 2017 15.9.41
SQL Server Analysis Services-Designer 15.0.19623.0
SQL Server 2017 (SSDB) 14.0.3370.1 (CU22 + Security Update)
SQL Server 2017 (SSAS) 14.0.249.62 (CU22)
Related
While using Power BI for a few months now, we (the user group) encountered an issue that is not really clear to us.
We use Power-BI with a remote SQL-Server data source, we access the data source through direct query.
Let's pretend we have 2 Tables as below-
Table name: Issue
Column:
ResolutionTime(Date/Time)
IssueID(Unique Numbers)
Table Name: WorkItem
Column:
start (Date/Time)
end (Date/Time)
IssueID (Unique Numbers, Foreign Key to "Issue" table)
Table WorkItem also contain a calculated column "WorkTime" which uses this DAX-expression as below-
WorkTime = WorkItem[end] - WorkItem[start]
The two tables are configured through Power-Bi having a two-way 1:n relationship that can be queried to collect all "WorkItem"(s) assigned to an "Issue" entry, using the "IssueID" as correlation column.
To be able to compute the aggregated "work-time" for each "WorkItem", we use a new/calculated table with the following DAX expression to aggregate the total amount of time invested for a single "Issue":
SumWork =
SUMMARIZE(
WorkItem, WorkItem[IssueID], "All work per item", SUM(WorkItem[WorkTime])
)
The above table computes the total invested work-time for a particular issue, grouping/summarizing results based on the "IssueID" foreign key. This new calculated table is also configured to have a relationship with the "Issue" table, this time a "1:1" relationship, using the IssueID as correlation column.
Now to compute the time that the issue was worked on + the time for Resolution should be summarized in a calculated column inside "Issue", but this does not work:
ResolutionAndWorkTime = Issue[ResolutionTime] + SumWork["All work per item"]
But the above DAX expression fails to compile, as it always reports that it returns "more than one result", thus not being a singular result. But that is suprising, as the two table ("Issue" and "SumWork" are related to each other with a "1:1" relationship).
Tables:
Issues
IssueID ResolutionTime ResolutionAndWorkTime
1 03:20:20 ???
2 01:20:20 ???
3 00:20:20 ???
WorkItem
IssueID start end WorkTime
1 1-2-2020 3:20:20 1-2-2020 3:25:20 00:05:00
1 2-2-2020 6:20:20 2-2-2020 7:20:20 01:00:00
3 1-3-2020 3:20:20 1-3-2020 3:29:20 00:09:00
Any ideas what to look for? Data-types? Table-definition? Table-relationships? We checked other Stackoverflow questions/answers, but no good ideas retrieved so far.
NOTE that a lot of join/merge features of Power BI are not available if direct-query is used and thus joining the tables is not really an option (we think).
You need this following code for your new Calculated column.
Visit HERE To know more about RELATED.
ResolutionAndWorkTime = Issues[ResolutionTime] + RELATED(SumWork[All work per item])
Based on input provided by "mkRabbani" (see other answer) we investigated why "RELATED" does not function as expected. The problem originates in the access to the database. As suspected earlier the function delivers the expected results once the database access is switched to "import" instead of "direct-query".
As a workaround we now joins the data inside the SQL server by using traditional database views. Of course this only works for scenarios where the database is under control of the data analytics team.
I try to get some data from tab A to tab B using the query function. This all works fine untill I include some data of tab B into the object in the query function.
The thing is, I want to create a scoring sheet that calculates the constructor points based of the first sheet with the driver standings. So tab A looks something like this:
Peter Wright - Team Fast Racers - 85
Cameron Bright - Team Quickies - 60
Marc Feiner - Team Fast Racers - 20
In this example the number correspond to a total amount of points on the right column.
The second tab needing to calculate the constructor points, looks simply like this:
Team Fast Racers 105
Team Quickies 60
Don't look at the amounts. It is just an example.
Now I want to use the query code where I can ask Google Sheets to grab the constructor name from tab B (i.e. Team Fast Racers) and look it up in tab A to get the score from both drivers and sum it up to get the total. This is the syntax now:
=QUERY({'Driver Standings'!$A$4:$T$32; 'Constructor Standings'!A1:C16}; "SELECT T WHERE C = 'B2'", 1)
The problem is that it does not recognize B2 to get the 'Team Fast Racers' name and search for it in tab A. How can I fix this problem.
Again, 2 drivers are in a team and those scores need to be summed up and displayed in the second tab to get a clean constructor championship standings. How can I get the data from tab A based on searching for a string out of tab B column B.
And if that is all done, I would also like the constructor standings (tab B) to be sorted based on amount of points, without losing the structure of the formulas.
first of all, all ranges in { } needs to be of the same size (at least in one dimension). if you are stacking ranges one under another then amount of columns needs to be the same:
{Sheet1!A:C; Sheet2!A:C}
then when you have a "constructed range" as above, you need to use Col references in QUERY statement
so instead of
"select A,C"
it needs to be
"select Col1,Col3"
then if B2 is a numeric number it will be
"where Col1 = "&B2
but if it is a plain text it will be:
"where Col1 = '"&B2&"'"
UPDATE:
tho all you need is:
=QUERY({'Driver Standings'!C4:C\'Driver Standings'!T4:T};
"select Col1,sum(Col2)
where Col1 is not null
group by Col1
order by sum(Col2) desc
label sum(Col2)''")
I'm working with an Excel sheet with more than 2,000 rows. The image I've attached shows the problem. I've checked the questions on StackOverflow & I don't see anything addressing this situation:
I need a formula to calculate the percentage in column E (E is formatted as a percentage). I can do this by hand logically but it takes upwards of 15 minutes and the report needs to be produced daily. The list comes from SQL Server and I can't get my SQL query to handle this either, nor can I make it work in VBA for Excel.
Logic for Cell E3:
If C2='house' and C3='house' and C4='house', then '100'
If C2='house' and C3='house' and C4='prospect', then 'C3/SUM(C3:C4)'
If C2='house' and C3='prospect' and C4='house', then 'C3/SUM(C2:C3)'
If C2='house' and C3='prospect' and C4='prospect', then 'C3/SUM(C2:C3)'
If C2='prospect' and C3='prospect' and C4='prospect', then '100'
If C2='prospect' and C3='house' and C4='house', then, '100'
If C2='prospect' and C3='house' and C4='prospect', then 'C3/SUM(C3:C4)'
If C2='prospect' and C3='prospect' and C4='house', then '100'
Thank you, in advance, for any assistance!
Obviously there was a typo in your question, you meant to calculate with numbers on column D not C, i.e D3/Sum(D3:D4) etc.
You could try to simply write that logic with nested if's... But I realize that the rule, which looked like a digital logic exercise, is actually much simpler. In summary the equation should detect the flip from "House" to "Prospect", if any.
Try this formula at E3 and copy it down:
E3:
=D3/(D3+ (D2*(C2="House")*(C3="Prospect")) + (D4*(C3="House")*(C4="Prospect")))
I am trying to move reports that currently run in SQL Server to Crystal Reports.
Essentially the statement I want to reproduce is:
SELECT DATEPART(DD,DATE), COUNT(*)
WHERE FOO = 'BAR'
GROUP BY DATEPART(DD,DATE)
Count the occurrence of records that match a criteria, grouped by date.
I have used the Selection Expert to generate a equivalence relation (to evaluate the records) and would like to use the datepart function in a group by statement. I have gotten the GROUP BY selection expert to group by date - but it is the full timestamp (SS:HH:DD:MM) not by specific day i.e. March 1 2010.
I am sure there is a way to achieve what I want but have yet to find a tutorial explaining this scenario.
Any help you could lend would be appreciated
As with any other SDK/Language, there are many ways to do this. Here is the first one that I can think of:
Get the raw data into Crystal. (Sounds like you already did this)
In Crystal, make a new formula, call it "GroupByDate". In the formula editor, enter:
datepart("yyyy-mm-dd",{mytable.mydatefield})
Go into the Group Expert. Group your report by GroupByDate.
Make a new formula, call it "AddMe". In the formula editor, enter:
iif({mytable.foo="bar",1,0)
Drag & drop your AddMe formula into the details section. Right-click on it to Insert->Summary. Set your summary location as the group footer.
Preview your report and you should see the total counts in every group footer. To simplify the appearance of the report, you can also suppress the display of the detail and grouper header sections.
Again, there are many ways to do this. You can also get creative with a Running Total function. The Crystal Formula Editor has very useful help files. Use the Functions pane to select a function, press F1, and you'll get criteria, examples, etc.
The problem that I have is SQL Server Reporting Services does not like Sum(First()) notation. It will only allow either Sum() or First().
The Context
I am creating a reconciliation report. ie. what sock we had a the start of a period, what was ordered and what stock we had at the end.
Dataset returns something like
Type,Product,Customer,Stock at Start(SAS), Ordered Qty, Stock At End (SAE)
Export,1,1,100,5,90
Export,1,2,100,5,90
Domestic,2,1,200,10,150
Domestic,2,2,200,20,150
Domestic,2,3,200,30,150
I group by Type, then Product and list the customers that bought that product.
I want to display the total for SAS, Ordered Qty, and SAE but if I do a Sum on the SAS or SAE I get a value of 200 and 600 for Product 1 and 2 respectively when it should have been 100 and 200 respectively.
I thought that i could do a Sum(First()) But SSRS complains that I can not have an aggregate within an aggregate.
Ideally SSRS needs a Sum(Distinct())
Solutions So Far
1. Don't show the Stock at Start and Stock At End as part of the totals.
2. Write some code directly in the report to do the calc. tried this one - didn't work as I expected.
3. Write an assembly to do the calculation. (Have not tried this one)
Edit - Problem clarification
The problem stems from the fact that this is actually two reports merged into one (as I see it). A Production Report and a sales report.
The report tried to address these criteria
the market that we sold it to (export, domestic)
how much did we have in stock,
how much was produced,
how much was sold,
who did we sell it to,
how much do we have left over.
The complicating factor is the who did we sell it to. with out that, it would have been relativly easy. But including it means that the other top line figures (stock at start and stock at end) have nothing to do with the what is sold, other than the particular product.
I had a similar issue and ended up using ROW_NUMBER in my query to provide a integer for the row value and then using SUM(IIF(myRowNumber = 1, myValue, 0)).
I'll edit this when I get to work and provide more data, but thought this might be enough to get you started. I'm curious about Adolf's solution too.
Pooh! Where's my peg?!
Have you thought about using windowing/ranking functions in the SQL for this?
This allows you to aggregate data without losing detail
e.g. Imagine for a range of values, you want the Min and Max returning, but you also wish to return the initial data (no summary of data).
Group Value Min Max
A 3 2 9
A 7 2 9
A 9 2 9
A 2 2 9
B 5 5 7
B 7 5 7
C etc..
Syntax looks odd but its just
AggregateFunctionYouWant OVER (WhatYouWantItGroupedBy, WhatYouWantItOrderedBy) as AggVal
Windowing
Ranking
you're dataset is a little weird but i think i understand where you're going.
try making the dataset return in this order:
Type, Product, SAS, SAE, Customer, Ordered Qty
what i would do is create a report with a table control. i would set up the type, product, and customer as three separate groups. i would put the sas and sae data on the same group as the product, and the quantity on the customer group. this should resemble what i believe you are trying to go for. your sas and sae should be in a first()
Write a subquery.
Ideally SSRS needs a Sum(Distinct())
Re-write your query to do this correctly.
I suspect your problem is that you're written a query that gets you the wrong results, or you have poorly designed tables. Without knowing more about what you're trying to do, I can't tell you how to fix it, but it has a bad "smell".