Generating Working Hours using SQL Server Query - sql-server

I have this data and I need to generate a query that will give the output below

You can do this kind of groupings of rows with 2 separate row_number()s. Have 1 for all the data, ordered by date and second one ordered by code and date. To get the groups separated from the data, use the difference between these 2 row_number()s. When it changes, then it's a new block of data. You can then use that number in group by and take the minimum / maximum dates for each of them.
For the final layout you can use pivot or sum + case, most likely you want to have a new row_number for getting the rows aligned properly. Depending if you can have data missing / not matching you'll need probably additional checks.

Related

Google Data Studio : how to obtain a SUM related to a COUNT_DISTINCT?

I have a dataset including 3 columns :
ID transac (The unique ID of the transaction - Dimension)
Source (The source of the transaction - Dimension)
Amount € (The amount of the transaction - Stat)
screenshot of my dataset
To Count the number of transactions (for one or more sources), i use COUNT_DISTINCT function
I want to make the sum of the transactions amounts (for one or more sources). But i don't want to additionate the amounts of the transactions with the same ID !
Is there a way to do this calcul with a DataStudio function ?
Thanks for your answers. :-)
EDIT : I saw that we could do this type of calculation via SQL here and I would like to do this in DataStudio (so that I don't have to pre-calculate the amounts per source.)
IMO, your dataset contains wrong data. Each value should be relative only to that line, but this is not the case: if the total is =20, each line should describe the participation of that line to the total. With 4 sources, each line should be =5 or something else that sums 20.
To solve it in DataStudio, you need something like CALCULATE function in PowerBI, but currently DataStudio doesn't support this feature.
But there are some options to consider to repair your data:
If you're sure there are always 4 sources, just create a new calculated field with the expression Amount/4 and SUM it. It is not an elegant solution, but it works.
If your data source is Google Sheets, you can easily repair the data using formulas, like in this example:
Link to spreadsheet
For this spreadsheet, I used this formula in adjusted_amount column: =C2/COUNTIF(A:A,A2). With this column in DataStudio, just use the usual SUM aggregation function to summarize it correctly.

PowerBI (DAX) - Countif Contains Column Value (thousands of rows)

Relatively new to PBI and need a hand with counting totals of delimited values in a single column. So my source column looks something like:
ID Code
1 abc1|bcd2
2 def2|abc1|ghi3
3 bcd2
I've created a new table based on the same query that takes just this column and splits it into individual rows by the pipe delimiter:
Individual Codes
abc1
bcd2
def2
ghi3
Now I'd like to plot the number of occurences of each individual code in the original code column. I had intended on doing this using a calculated column, but I don't know if that's even the best approach. So having something like:
Individual
Codes Counts
abc1 2
bcd2 2
def2 1
ghi3 1
If it's possible to relate the tables, I'm not sure how. I've tried filter approaches similar to this but that's caused crashes. The current source data has maybe 50k rows (with 8k individual codes), but potentially these values could be 10-100x larger so I imagine it's best to avoid something that's creating filters of the source data for each row of the Individual Code table.
Much appreciated!
Original Data
Then you can split Your code column into new rows using delimiter
and then you can group your rows based on Code and count rows as below
and when you come back to Visualizations you can have your desired output

Ssrs reports Windows 2008

I'm fairly new to SQL and I have been issued my first report to build. I have written an SQL query to give me a set of results that I would like to publish in a report.
I have unioned about 20 small queries all containing the correct amount of columns. One column is a misc column with about 15 different descriptions in (this is what I want to count).
I have uploaded my data set and now want to be able to choose a cell in my report to bring back a certain description.
At the minute I'm using
=count(fields!misc.values)
and it's giving me the whole count, about 200.
I would like to know if there is any kind of "where clause" (filter) which I can use to state which description results I want to bring back.
You can use am expression to count the misc.value you need. It will work like a count using a where clause:
=Sum(iif(Fields!misc.Value ="Some description",1,0))
Example:
For count the FSMethod with MethodOne as value I used this expression:
=Sum(iif(Fields!FSMethod.Value ="MethodOne",1,0))
Note the expression sums by 1 if the FSMethod.Value is MethodOne.
For count the rows with FSMethod column with MethodTwo value.
=Sum(iif(Fields!FSMethod.Value ="MethodTwo",1,0))
Let me know if this can help you.

Creating custom rollups with SSAS

I am currently working on a requirement as follows and would appreciate some help in figuring out a way to configure the aggregation of my measure:
I have a fact table that contains the following Item ID, DateID,StoreID, ReceivedComments. The way received comments work is that on a daily basis a new record is created that adds to the value of received comments (for example if Item 5 in Store 5 on 1 Jan had 23 Received Comments and it received 5 comments the following day, the row for Jan 2 would be Item 5, Store 5, Jan 2, 28)
We created a measure using MAX and it works fine whenever Item ID is used in the query. When we start moving to a higher level the max produces wrong results. Our requirement is to setup the measure to be as follows:
If the member selected is on the Item Level then MAX, if it's on any other level (Date or Store) then the measure should aggregate the Max of all Items under this date or store.
Due to the business rules and structure of the database Store and Item are different dimensions so I can not include them in 1 Hierarchy.
We have been playing around with Custom RollUps but so far haven't been able to get it to work.
Thanks
I would solve this by using a more traditional approach to your fact table. Instead of keeping a cumulative count in the ReceivedComments column, I would keep only the number of comments received THAT DAY.
That way, instead of using MAX, you can create your measure using SUM, and it will automatically rollup when you go to higher levels.
The only disadvantage I can see to this approach is that you will need to use a range of dates, instead of only the most recent date, to get a full total of all the comments for a given item/store/date. But that's a very small change to your MDX.
Someone suggested using ISLEAF to determine the level, Instead of using ISLeaf i went with AS CASE WHEN [Item].[ItemID].CURRENTMEMBER.LEVEL IS [Item].[ItemID].[(All)] so I don't have to account for other dimensions such as Date, Store, etc as I have several other dimensions that all behave the same way.
And then I went with this formula to determine the Sum of the Max of the items in a particular store like this:
SUM({[Item].[Item ID].children},[Measures].[ReceivedComments]), Now I expect some performance issues with this measure but we are currently running some tests to see if it's gonna be reliable to work with it on actual data.

SQL Server Reports Aggregate Functions

G'day,
I'm a total n00b when it comes to SQL Server reports and my Vb.Net knowledge is ageing now.
I have a detailed report of rows in a database, with one of the columns that is nullable. What I want to do is show a simple pie chart over the data in the result which shows how many rows have a value and how many do not.
I can't work out how to write the Count() aggregate in the expression for the data series so that it filters.
eg.
I have 10000 rows of which 2000 have a null. I want a pie chart that shows two results, one small pie chunk with 2000 and a larger pie chunk with 8000. When I try and do =Count(IsDbNull(Fields!TransactionID.Value)) and =Count(Not IsDbNull(Fields!TransactionID.Value)) it appears to send the same result twice, ie. the result set is still the same size it just consists of trues and falses.
Cheers for your help.
Since you are using COUNT you are just counting values; IsDbNULL is returning TRUE or FALSE both of which are being counted.
Try doing this for the Non-NULLS =SUM(IIF(ISNOTHING(Fields! TransactionID.Value),0,1)) and for the NULLs use =SUM(IIF(ISNOTHING(Fields! TransactionID.Value),1,0))
Actually, for the non-NULLs you can just use COUNT(Fields!TransactionID.Value)
I'd use something like this
-SUM(CInt(IsDbNull(Fields!TransactionID.Value)))
COUNT(Fields!TransactionID.Value) + SUM(CInt(NOTIsDbNull(Fields!TransactionID.Value)))
COUNT literally counts values, same as SQL Sever. So:
IsDBNull gives true/false -> -1/0 -> SUM that gives you number of NULLs (minus of course)
Then take full count, subtract the SUM gives non-NULL count

Resources