I have a fact table with columns as Quantity, Unit Price, etc. I am trying to calculate the revenue with the SUMX formula but I am getting the same value for all the records. And due to this I am also getting a dependency error in other column. Here is the code:
SUMX(
'''Sales Details$''',
'''Sales Details$'''[Quantity]*'''Sales Details$'''[Unit Price]
)
This table has been imported from SSMS as it is, into the tabular model analysis service in VS2019.
I wish to understand few things here-
Why we have to provide a table inside of 3-quotes? The DAX bar is not taking the table without specifying them under 3-quotes.
SUMX shouldn't evaluate the same value for all the records. But it is doing here for an unknown reason.
If I try to replace the [Unit Price] with [Unit Cost] in the upper code then I am getting a dependency error in the new column. As far as I know, I am not using a CALCULATE function which will generate circular dependency and SUMX doesn't puts the filter on columns, [Quantity] here.
I think it is because the table name has spaces. When a table name has spaces or not allowed characters it goes between two single quotes: ""
If I'm not wrong (I'm quite new with DAX too), SUMX is like sumproduct in Excel. It does the unit price * quantity per row and then sums up all the rows, breaking the row context. If you want to calculate the amount per row, just do price * quantity, without SUMX.
I don't know, sorry.
Related
I am trying to add a column to a collection by multiplying the 0.9 to existing database column recycling. but I get a run time error.
I tried to multiply 0.9 direction in the function but it is showing error, so I created the class and multiplied it there yet no use. what could be the problem?
Your error message is telling you what the problem is: your database query is using GROUP BY in an invalid way.
It doesn't make sense to group by one column and then select other columns (you've selected all columns in your case); what values would they contain, since you haven't grouped by them as well (and get one row returned per group)? You either have to group by all the columns you're selecting for, and/or use aggregates such as SUM for the non-grouped columns.
Perhaps you meant to ORDER BY that column (orderBy(dt.recycling.asc()) if ascending order in QueryDSL format), or to select all rows with a particular value of that column (where(dt.recycling.eq(55)) for example)?
I am trying to get sum of fact table column based on dimension table column value. ie.
If Dim_Product[Origin]="A"
THEN SUM(Fact_Connectivity[MONITOR_CNT])
ELSE
SUM(Fact_Connectivity[TLA_MONITOR_CNT]).
I am using below formula:
Adoption %:= IF(Dim_Product[Origin]="A",
SUM(Fact_Connectivity[MONITOR_CNT]),
SUM(Fact_Connectivity[TLA_MONITOR_CNT]))
But I couldn't use Dim_Product[Origin] table fields in the formula even though Fact table has relationship with Dim table.
You can't directly use it in a measure, raw, or using related, you will have to wrap it round with something to determine the row context.
So for example,
Measure = IF(MAX('Dim Origin'[Origin]) = "A"
, SUM('Table'[Monitor_cnt])
, SUM('Table'[TLA_Monitior_cnt])
)
So on a row by row basis It will take the MAX value for that row, then apply the function. AS the row will only have one value the logic works.
You could also move this to a calculated column.
Hope that helps
This is the data that comes back from the database
Data Sample for one season (the report returns values for two):
What you can see is groupings, by Season, Theater then Performance number and lastly we have the revenue and ticket columns.
The SSRS Report Has three levels of groupings. Pkg (another ID that groups the below), venue -- the venue column and perf_desc -- the description column linked tot he perf_no.
Looks like this --
What I need to do is take the revenue column (a unique value) for each Performance and return it in a separate column -- so i use this formula.
sum(Max(Fields!perf_tix.Value, "perf_desc"))
This works great, gives me the total unique value for each performance -- and sums them up by the pkg level.
The catch is when i need to pull the data out by season.
I created a separate column looks like this
it's yellow because it's invisible and is referenced elsewhere. But the expression is if the Season value = to the Parameter (passed season value) -- then basically pull the sum of each of the tix values and sum them up. This also works great on the lower line - the line where the grouping exists for pkg -- light blue in my case.
=iif(Fields!season.Value = Parameters!season.Value, Sum(Max(Fields!perf_tix.Value, "perf_desc")), 0)
However, the line above -- the parent/header line its giving me the sum of the two seasons values. Basically adding it all up. This is not what I want and also why is it doing this. The season value is not equal to the passed parameter for the second season value so why is it adding it to the grouped value.
How do I fix this??
Since your aggregate function is inside your IIF function, only the first record in your dataset is being evaluated. If the first one matches the parameter, all records would be included.
This might work:
=IIF(Fields!season.Value = Parameters!season.Value, Sum(Max(Fields!perf_tix.Value, "perf_desc")), 0)
It might be better if your report was also grouping on the Venue, otherwise you count may include all values.
Either I am the first person to ever need to display percentages in Tableau or I do not know what to search for! I highly suspect it is the latter...
I believe what I am attempting to ask is how to make a calculated non-aggregated field by dividing by an aggregated number. Although I would prefer just to be able to display the percentages instead of a whole number.
This is how I would do it in Excel:
The data that already exists is Column A and B. In Tableau these would be non-aggregated. What I need to do in Tableau is to generate what is column C (also non-aggregated) because it does not exist in my data. In excel, all I did to get the aggregate number (total) of column B was:
sum(B1:B4)
And for the column C:
=B1/$B$5
But I can't seem to do this at all in Tableau. When I try to use the same syntax, I get an error message: "Cannot mix aggregate and non-aggregate arguments with this function."
Instead of having a calculated field, you can use a Quick Table Calculation on the column.
Right-click the pill of your data > Quick Table Calculation > Percent of Total. This will show the percentages instead. If you want to keep both, just duplicate column b first and then add the table calculation to the new column.
A friend told me that his new employer needs an SSRS report that parse a column that contains n consecutive occurrences of
1) the literal "Date:"
2) An optional separator character
3) followed by a date in DD-MM-YY format (leading zeros are optional)
4) a separator space
5) A single "WORD" of data that is associated with the date. This word will have no embedded spaces.
I'll populate a Sample table with data that meets this critera to give you an example to make it clear:
CREATE TABLE [dbo].[Sample](
[RowNumber] [int] NOT NULL,
[DataMess] [varchar](max) NOT NULL
) ON [PRIMARY]
INSERT [dbo].[Sample] ([RowNumber], [DataMess]) VALUES (1, N'Date:12-21-13 12/13/14/15 Date:4-2-11 39/12/134/14 Date:4-1-13 19/45/5/12')
INSERT [dbo].[Sample] ([RowNumber], [DataMess]) VALUES (2, N'Date:7-21-13 12/13/14/15 Date:8-21-12 39/12/34/14 Date:12-1-13 19/4/65/12')
INSERT [dbo].[Sample] ([RowNumber], [DataMess]) VALUES (3, N'Date:3-21-13 12/11233/14/15 Date:4-28-13 39/12/34/14 Date:9-19-13 19/45/65/12')
For the first record, "12/13/14/15" is considered to be the "Word" of data that is associated with the Date 12-21-13.
He was aked to produce the following report in SSRS:
Row Number DataMess
1 Date: 12-21-13 12/13/14/15
Date: 4-1-13 19/45/5/12
Date: 4-2-11 39/12/134/14
2 Date:12-1-13 19/4/65/12
Date:7-21-13 12/13/14/15
Date:8-21-12 39/12/34/14
3 Date:9-19-13 19/45/65/12
Date:4-28-13 39/12/34/14
Date:3-21-13 12/11233/14/15
Note that the Dates for each source row number are sorted in descending arder alomng with the associated wor of data.
I don't know SSRS, but my reaction was to recommend to him that he not even attempt the task but to tell his employer that the data shouldn't really be trying to do all of that ugly string parsing with T-SQL. Instead this repeating "Date: DATA" should be stored in individual child records that are associated with a parent Row record. I believe that the code would be ugly, inefficient, brittle and hard to maintain. What are your thoughts?
Assuming that management\client is always right or to conceed that "ideally" this is correct, but "for now" we need a SQL that produces the following report, how would one do this? The expectation was that this can be produced quickly ( a half day, for example)
You are of course correct, it's certainly far from the best way of storing the data. Any way of extracting the data for this report will be much more complex than it would be if it was stored differently.
However, based on the data it still wouldn't be too tough to actually generate the report. Due to the table structure actually generating the dataset for the report would be the hardest part.
So to generate the dataset, you need to split the data in DataMess to get one row per Date/Word, and be able to extract the date from that split data to be able to order by date as required.
Take your pick on how you want to split the data:
Split function equivalent in T-SQL? has many options, as does this link - Best Split Function.
Here's a SQL Fiddle with one of the functions in action.
Once you've split the data, use the appropriate function to extract the date portion, i.e. between the colon and the space before the word data, then CAST this as a date.
Once you've actually got the dataset, it's the most trivial of reports - just add a row group based on RowNumber, add the split Date/Word data as a detail field and you're done. Make sure the dataset is ordered by the extracted date field, even though you don't actually display this in the report.
As an interim measure I would certainly expect this to be doable in half a day of work or so. So for just this report it's not too bad, but for anything else you'll probably have trouble.
For a few rows it will likely run fine, but on any non-trivial sized dataset performance will be suboptimal.
Thank you. Here's what I did for the remaining part to get the dates sorted in DESC order.
SELECT
RowNumber
,'Date: ' + ss.Item AS Data
--,cast(substring(ss.Item, 1, charindex(' ' , ss.Item) ) AS date)
FROM
Sample s
CROSS apply dbo.SplitStrings_XML(s.DataMess,
N'Date:') ss
WHERE
Item IS NOT NULL
ORDER BY
rownumber,
cast(substring(ss.Item, 1, charindex(' ' , ss.Item) ) AS date) desc
If the data fails to hold up to this expected format and we encounter a date that is not valid, then the whole report blows up.