Previous years registered: Excel COUNTIF and RETURN for X years - arrays

UPDATED screenshot after attempting #Dude_Scott's suggestion:
Desired output of data is in the blue table.
Our data includes users who have registered between 1989-2016.
All have registered at least once.
Some register every year and some skip years.
Our question is for each year, find how many years previous users registered.
We want results to be 0yr, 1yr, 2yr, 3yr, etc., for each year.
Arrays are working correctly.
I've organized the data structure in Excel this way:
1) UserID All Years
For year 1989, result is 0, since it was the first year of data collection.
For year 1990, this formula returns the expected count:
=COUNT(IF($B$2:$B$11613=1989,1/COUNTIFS($A$2:$A$11613,$A$2:$A$11613,$B$2:$B$11613,1989)))
Beginning with year 1991 is where I am tripped up: I can't find for multiple years.
This formula is not working:
=COUNT(IF(AND(OR($B$2:$B$11613=1989,1990,1/COUNTIFS($A$2:$A$11613,$A$2:$A$11613,$B$2:$B$11613,1989,1990)))))
Where do I argue "COUNTIF 0 yr, 1 yr, 2 yr", etc. Thanks in advance. --f66

Since your using COUNTIF, I'm assuming you can use SUMPRODUCT also. In the below screen shot I am using this formula
=SUMPRODUCT(($A$2:$A$11=$D3)*($B$2:$B$11<=F$2))&" yr"
Without a sample of the workbook its a little difficult to determine what the output of your data should look like, but give it a go.
Side note, I would suggest updating the User ID and Years into a table format and giving it a named range, so you don't iterate though tens of thousands of lines with the array formula.

I may be reading this all wrong, but this seems to be a straightforward use case for a pivot table!
Select your data range
Set USERID as your row headings
Set YEARS as your column headings
Set count of USERID as your values
I do not use excel anymore, but below is a link to output for doing this with test data on google sheets.

Related

EXCEL: How to count if data matches in specific columns where header is within the date range?

I would like to get a count on the total number of lessons of a specific length given within a specific date range.
I can figure out how to get the number of a specific type of lesson on a specific day with something like in:
=countifs(INDEX($E:$V,,MATCH($A8,$E$1:$V$1,1)),"=30")
But I can't figure out how to to find all of the values for say Dates <=A8 (for row 8), or dates >A8 & <=A9 (for row 9).
I am looking to get the data to output like the yellow section.
In Excel:
=countifs(INDEX($E:$V,,MATCH($A8,$E$1:$V$1,1)):INDEX($E:$V,,IFERROR(MATCH($A9,$E$1:$V$1,1)-1,COLUMN($V$1))),"=30")
But note, this will not work in Google Sheets.
I'm not sure it is the most elegant. My solution was to create a hidden column (Now E:E) for the to calculate the days between the A column dates.
Example for cell E9
=SUMPRODUCT(($F$1:$W$1>A8)*($F$1:$W$1<A9))
This returned a number of days between days A7 and A8.
I then modified Scott Craner's formula to this (Example for cell B9):
=COUNTIFS(INDEX($F:$W,,MATCH($A9-$E9,$F$1:$W$1,1)):INDEX($F:$W,,IFERROR(MATCH($A9,$F$1:$W$1,),COLUMN($W$1))),"=30")
Remember now, that after adding a column my dates shifted from E1:V1 to F1:W1
These two steps solved my issue with double counting on certain dates.
Lastly, I went back and tested this with Google Sheets and it does now appear that it will also work with Google Sheets.

Create a column within an SSRS Report to sum values based on conditions of previous column

I have an ssrs report that shows revenues for each month of a year for several years based on start and end date parameters the user enters. What I would like to do is add a second column that shows the revenue for that quarter (march would show q1 revenue, june q2 etc). the problem I am having is that I am able to do this for a single year but once the query starts looking at multiple years my column shows the value for all 1st quarters regardless of year. The picture shows what I am currently getting. Any recommendations on how to fix my expression to also look at the year value when summing these quarters?
Here is the code I am using to only populate the quarter column based on the month.
=IIF(Fields!Sort_Order.Value=3,
Sum(Lookup(1,Fields!Sort_Order.Value,Fields!Calculated_Revenue.Value,"Bookings"))
+Sum(Lookup(2,Fields!Sort_Order.Value,Fields!Calculated_Revenue.Value,"Bookings"))
+Sum(Lookup(3,Fields!Sort_Order.Value,Fields!Calculated_Revenue.Value,"Bookings")),
IIF(Fields!Sort_Order.Value=6,
Sum(Lookup(4,Fields!Sort_Order.Value,Fields!Calculated_Revenue.Value,"Bookings"))
+Sum(Lookup(5,Fields!Sort_Order.Value,Fields!Calculated_Revenue.Value,"Bookings"))
+Sum(Lookup(6,Fields!Sort_Order.Value,Fields!Calculated_Revenue.Value,"Bookings")),
IIF(Fields!Sort_Order.Value=9,
Sum(Lookup(7,Fields!Sort_Order.Value,Fields!Calculated_Revenue.Value,"Bookings"))
+Sum(Lookup(8,Fields!Sort_Order.Value,Fields!Calculated_Revenue.Value,"Bookings"))
+Sum(Lookup(9,Fields!Sort_Order.Value,Fields!Calculated_Revenue.Value,"Bookings")),
IIF(Fields!Sort_Order.Value=12,
Sum(Lookup(10,Fields!Sort_Order.Value,Fields!Calculated_Revenue.Value,"Bookings"))
+Sum(Lookup(11,Fields!Sort_Order.Value,Fields!Calculated_Revenue.Value,"Bookings"))
+Sum(Lookup(12,Fields!Sort_Order.Value,Fields!Calculated_Revenue.Value,"Bookings")),""))))
I ended up creating a quarter column within my data set that was then used as a parent group for the month rows. I then hid that column and added a total for the quarter that gave me sub totals. Not exactly what I was looking for but gave me the result I needed.

ValueFilter for DateTime Attributes

I'm working with the Blog app and I see how to filter the Blog posts by year using the Visual Query Designer. I use the querystring value that has the year and in the ValueFilter and my properties are as follows:
Attribute: PublicationMoment
Value: [QueryString:year]-01-01 and [QueryString:year]-12-31
Operation: between
How would I get the posts from a specific month and year, if those values are passed via query string parameters. Because the months of the year have a varying number of days, I'm not sure how you would accomplish this in the Value field of the ValueFilter. Currently I'm passing the 2 digit month as the parameter.
I tried something like: [QueryString:year]-[Querystring:month]
Operation: contains
but the above operation doesn't really work because the datatype is a DateTime object.
I could do it in the razor view but I'm afraid that the paging datasource would have too many pages in it since it would be based on the larger subset of posts for the given year that was passed in the querystring parameter.
Is there any way to do this with the filter?
Basically dates are not perfectly handled yet, but there are a few ways to do it using the visual query:
Use the correct date in the query like between [QueryString:Start] and [QueryString:End] and calculate the correct dates there where you generate the links
Since your main problem with the "between" filter is actually that it would include the last day too, you could also use a two filters a >= first date and another < second date, so the first-date would be the year/month and day 1; the second one is year-month and day 1 as well
Last but not least: if you do it with razor and LINQ you shouldn't run into any performance issues - it's technically the same thing the pipeline does and it's been tested to perform well with tens of thousands of records.

Is it possible to create an SQL query that displays results like this?

Background
I have a database that hold records of all assets in an office. Each asset have a condition, a category name and an age.
A ConditionID can be;
In use
Spare
In Circulation
CategoryID are;
Phone
PC
Laptop
and Age is just a field called AquiredDate which holds records like;
2009-04-24 15:07:51.257
Example
I've created an example of the inputs of the query to explain better what I need if possible.
NB.
Inputs are in Orange in the above example.
I've split the example into two separate queries.
Count would be the output
Question
Is this type of query and result set possible using SQL alone? And if so where do I start? Would it be easier to use Ms Excel also?
Yes it is possible, for your orange fields you can just e.g.
where CategoryID ='Phone' and ConditionID in ('In use', 'In Circulation')
For the yellow one you could do a datediff of days of accuired date to now and divide it by 365 and floor that value, to get the last one (6+ years category) you need to take the minimum of 5 and the calculated value so you get 0 for all between 0-1 year old etc. until 5 which has everything above 6 years.
When you group by that calculated column and select the additional the count you get what you desire.

How do I get around the Sum(First(...)) not allowed limitation is SSRS2005

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".

Resources