Using running totals in MS access report cumulatively - ms-access

I am developing a db (MS access 2010) to support a school with a well-defined model for tuition quotation. The list of products is assembled for each quote, then various discounts are applied. The discounts may be a percentage or an absolute dollar amount. So far, so easy. The problem is that their business logic requires:
No limit on number of discounts.
Specific discounts to be applied in a defined sequence (implemented in my case with a "discount ordinal" column, values 1 (first applied) to 100 (last applied).
Each sequential application of a discount is to the running total of the quote. Eg: Total products $1000. Discount: 50%. Value: $500. Subtotal $500.
Subtotal: $500. Discount: $25. Value: $25. Subtotal: $475.
Subtotal: $475. Discount: $10%. Value: $47.50. Subtotal: $427.50.
This appears to be a variation of the "get the value of the field in the previous row" problem, but with the added twist that the "value of the field" is actually a cumulative calculation. It has the flavor of recursion: while discounts remain, subtotal(previous subtotal).
I have no clear idea how to implement this in a report, because the calculation as noted above is self-referential. I'm not looking for code here, just guidance on the general approach to the problem (ie, some kind of global variable, using VBA - in which case, I'm not sure what the "glue" between the query in VBA and the report would be - or some trick with a calculated field although I've spent a lot of time trying to figure one out). Any thoughts?

In that kind of situations, I always create a new table, that will get filled up when the report opens, and base the report in that table, not the original one. That way I can do all the calculations I need, even making several passes. The report then is simply a "dump" of the table. Complex totals can be additional columns, that will be shown only in the totals section.

You could have table for purchase history using an integer to link each purchase since an autonumber by itself will not link each discount stage.
So in excel I would use something like this:
v = Starting Value
i = 1
Do Until i = Last Discount
d = ws.Cells(i, 9).Value
v = v * (1 - d)
ws.Range("B2").Value = v
i = i + 1
Loop
At each stage you could write to the table (using docmd.runsql) the discount applied (d) and the value (v) but it could be quite slow. You could then order the table by purchase identifier then descending by value since sequential discounts will inherently order correctly.

Related

get total from two different datasets into a third different tablix ssrs

Here I am, again with another doubt.
Here is my problem:
I have a report where I show ALL the incomes and expenses of the company for the last two days, which i managed by placing a column group grouping the columns by the date
Originally, the report contained a single matrix with both incomes and expenses fed by a dataset that points to the company cube applying the following filter expression
=IIF(Fields!Fecha.Value=Parameters!FechaHoy.Value or Fields!Fecha.Value = Parameters!DiaAnterior.Value,true,false)
FechaHoy = date sent via report parameter input
FechaAyer = date
parameter minus one day
and the filter value set to
=true
So far so good. Today, the upper management decided to split it into three matrix. One for incomes and other two for expenses (one for each expense category), so i startet with this
initial report layout
I created two new datasets for each one of the expense categories with the same filters, and now i'm here
current report layout
THE THING IS... as you can see, there is a "Total Destino" independent table where i have to add the totals of the second and third matrix in the image I added, but, as I already said I have to show the last two days, so i also have to partially add the total of each day (which are column grouped by date)
I'm pretty sured that there's also a better and more efficient way to manage what I did with the three matrices but the main issue which needs your assistance is how do i add the totals of the second and third matrices.
I already tried with
=Sum(Fields!Saldo.Value, "Informacion_Destino") + Sum(Fields!Saldo.Value, "Info_InvTesoreria")
but it adds the two days and repeat it for the two days. I also tried with ReportItems!TextBox + ReportItems!TextBox but the preview loads sends me an error message.
Thanks in advance for your time and help
Like Harry stated already out you can use for your overall total the following expression for the Tesoreria Subtotal + Destino Subtotal :
=ReportItems!TesoreriaSubtotal.Value + ReportItems!DestinoSubtotal.Value
This would give you the overall total for all days. Now if you just have to show the last two days you can add a tablix filter, like this:
'Expression
=IIF(CDate(Fields!YourDate.Value) < DateAdd("d", -2, Now()), True, False)
'Format
=Boolean
'Value
=False

query of calculate the annual profit growth in qlikview by using text object

I want to calculate the annual average profit growth rate for 5 years by using a text object. Qlikview does not seem to support nested aggregation... I am new to QlikView, can anyone can help me?
Tried query:
='Annual Growth Rate (2003-2007) :' & avg((sum(Profit_per_Product)/sum(Total_Retail_Price))*100)
Thanks in advance!
QlikView does support nested aggregation and can fulfil your requirements if you utilise the AGGR function. This function allows you to perform aggregation over a set of dimensions that you specify and in its simplest form, can be use as follows:
=aggr(expression,dimension)
This means that expression is evaluated for each value of dimension. For example, in your case if you used sum(Profit_per_Product)/sum(Total_Retail_Price) as your expression, and used your dimension that contained your data's year, then aggr would result in five different values (one for each change in your year). As you wish to take the average, then the AVG function happily takes these five values and provides you with your desired result.
Therefore, all you need to do is to slightly change your expression to the following (here I assumed that your dimension that contains the year is named Year):
='Annual Growth Rate (2003-2007):' & num(avg(aggr(sum(Profit_per_Product)/sum(Total_Retail_Price),Year)),'0.00%')
I also added the NUM function to format your result as a percentage.
More information on Advanced Aggregation (i.e. AGGR) may be found in section 25.2 of the QlikView Reference Manual (in my copy it's on page 291).

SSRS how to get a running total of inventory amounts IN and OUT from START DATE to END DATE?

i have a stored proc returning inventory amounts between a start and end date.
the QTY field shows on-hand inventory adjustments by date, either IN or OUT.
i have a "run balance" field that needs to show the total QTY each day. if the InOut field is 0, it's QTY in. if it's a 1, its QTY out. the values reflected in In and Out are coded that way.
EXAMPLE. if i start on 4/1/14 with 5216. then 1061 of product is made on 4/1/14 i need the run balance to take 5216 and add 1061 on the next line. the next line should read 6277 instead of 1061. then if some is shipped, take 6277 and subtract, and so on.
i tried the running value on QTY, but it just repeats the QTY. doesn't calculate anything.
=RunningValue(Fields!QTY.Value,Sum,"Details")
i tried to post an image of my report preview, but i don't have the reputation ;-)
any help would be MUCH appreciated.
EDIT: OK, i tried this code block:
= Switch(RunningValue(Iif(Fields!InOut.Value = 0, Fields!QTY.Value,0),Sum,"Details"),
RunningValue(Iif(Fields!InOut.Value = "1", Fields!RunBalance.Value - Fields!QTY.Value,0),Sum,"Details"))
and i am getting a 0 on the sum fields, and #Error on the subtraction fields. but, i think this is more the direction i need to go (i hope)....but i am still not getting it.
There are a couple of things to look at here.
First, you specify the Scope of "Details" in your RunningValue expression... By default this group will have no grouping value, i.e. each row in the dataset will be in its own group. Which means the RunningValue will only ever be applied to one row only. Change the Scope to "MyDataset" or Nothing, whatever is appropriate.
Secondly, you need to consider how the InOut field affects the RunningValue.
I have created a simple Dataset:
And a simple table:
The Balance expression is:
=RunningValue(IIf(Fields!InOut.Value = 0, Fields!QTY.Value, Fields!QTY.Value * -1)
, Sum
, Nothing)
You can see this changes the Scope from your original statement, and also applies a multiplier to QTY based on InOut.
Works OK for my sample data:

SSRS subtotalling columns with expressions

Using Visual Studio 2010 and SQL Server 2012
I have made an SSRS report that requires a custom method to subtotal and arrive at a grand total for Percent of Assets and other calculations derived from Percent of Assets. I cannot simply use the Add Total Function because it is grayed out as is typical when summing expressions. I cannot manually calculate the subtotals because I get a nested aggregate error.
The software I'm using to make this SSRS report provides various stored procedures and functions to make reports using derived computations. The stored procedure contain a Percent of Assets column but because the report is filtered to show only common stock, the percentage of Assets function is incorrectly still counting unfiltered assets in its calculation. The assets under consideration make up 59.7 % of total holdings but I need the report to behave as if they were 100% of holdings.
To understand some of the math in this report please look at the following report output. Note that the report I want only includes common stock which make up 59.7 Percent of total assets as mentioned.
This report represents the desired output which is currently being calculated in a different reporting program. The report has two tables
% Company Sales is a hard coded value that represents the percent of sales that are either domestic or international. You see this and % Portfolio Assets in both the international and domestic tables.
% Portfolio Assets is calculated by multiplying the percent of assets of a given holding by the hard coded company sales value. In the first example McDonald's 2.1% Portfolio Assets is 6.6% * 32%.
6.6% is the percent of Assets McDonald's represents in the portfolio is if you only count common stock (exclude cash)
McDonald's Value Total Portfolio Value %
4,950.00 / 73,919.50 = 6.6000%
Domestic/ International Percentage breakdown
Total % Sales %Split Region
6.60% 32% 2.1% Domestic
6.60% 68% 4.5% International
Now here is my report output. It differs slightly from the design layout I will be showing because I added columns and changed the headers for explanatory purposes in this preview example. So far I am just trying to get the Domestic table to work. Once that is working I will build the international table. I have a few extra colums that I'm using for troubleshooting.
I have tried several methods to Calculate Portfolio Assets. I can get the individual row values to be correct but I cannot get the subtotal value to be correct using different methods due to a nested aggregate error (summing and already summed expression when a data scope is used) The grand total is also important but for now I'm just trying to get subtotal working.
Here is one method to get the row detail value for Percent of Assets. This is used in the calculated Percent Column. This column is the one that won't allow me to subtotal without a nested aggregate error.
When I try to use the following expression to get the subtotal I get the nested aggregate error
=SUM(Fields!MarketValue.Value/Sum(Fields!MarketValue.Value, "DataSet1"))
ERROR Error 2 [rsInvalidNestedDataSetAggregate] The Value expression for the text box ‘Textbox39’ has a nested aggregate that specifies a dataset scope. Inner aggregates cannot specify a dataset scope.
If use the PercentAssets field which is included in the Stored Procedure I can subtotal and get grand total without errors but the value is the filtered value which only sums to 59.7 if I multiply this filtered percent by the company sales the value is wrong.
Last I tried to use custom VBA code to get my subtotal and grand totals. However this provides a running total of the calculated percents not the subtotals I need for the SectorName groups.
Here is the VBA Code I used which I found on the web here:
http://social.msdn.microsoft.com/Forums/sqlserver/en-US/1d3c5ec1-89cb-4108-8637-ff434027b1ec/sum-an-expression-value-ssrs
Dim public nettotal as Double
Public Function Getvalue (ByVal subtotal AS Double) AS Double
nettotal = nettotal+ subtotal
return subtotal
End Function
Public Function Totalvalue()
return nettotal
End Function
Code deployed
In summary I need to multiply the calculated Percent column's values by the Company sales. I need to subtotal the result of these calculations.
If you look in the Information Technology Sector Grouping you can see the original report correctly arrives at 32.8 as the subtotal.
My report incorrectly arrives at 100% because that is the running total tally of the Calculated percents.
Here is a link to the original rdl
https://dl.dropboxusercontent.com/u/87501202/InternationalDomestic.rdl
It looks like your code:
=SUM(Fields!MarketValue.Value/Sum(Fields!MarketValue.Value, "DataSet1"))
isn't quite correct... Try this instead:
=SUM(Fields!MarketValue.Value)/Sum(Fields!MarketValue.Value, "DataSet1")
Breaking it down slightly, the second piece of code is expression / expression; now it's just two SUM expressions with different scopes as required - the first one looks like the syntax is off slightly, hence your error.

SSAS :: MDX :: Scope function :: Exchange Rates

Hey all,
My problem:
I'm trying to create a scope function that calculates exchange rates based on a date and currency AND a specified rate.
I have this working fine, but within my scope function I want to say - if Dimension.Attribute.member = "Latest" then use FXRate 1 otherwise use FXRate 2.
Now I even have that "working"... BUT that only works if the member is in my dataset... i.e. I can't use it as a parameter\filter.
I don't want to have to tell the users "you always have to have LatestFlag in every report... just hide the column"
I want to give the user the ability to set the report parameters before he starts analysing the data.
So here's a snippet of my code so far:
Scope ( { Measures.[Amount]} );
Scope( Leaves([ExchangeDate]), [Reporting Currency].[USD],Leaves([Currency]));
Scope( { Measures.[Amount]});
This = iif(
[Latest Flag].[Flag].CURRENTMEMBER.name = "Yes",
[Reporting Currency].[Local] / Measures.[Rate2],
[Reporting Currency].[Local] / Measures.[Rate]
);
End Scope;
End Scope;
End Scope;
I suspect I need to use another Scope instead of the iif - but I'm not sure how to implement.
Any ideas?
Oh it's probably important to note.
The FXRate table has two rates.
Rate is updated daily.
Rate2 is repeated for every currency everyday.
So irrevelant of the date, Rate2 will always be the latest rate for that currency.
The LatestFlag dimension is merely a table with yes and no and doens't have any relationship to any other table.
I'm just using it as a filter.
There is a flag on the FX table too - but I'm not using this as I need the date to be considered if it's not the latest rate.
My solution was solved in the following link.
http://social.msdn.microsoft.com/Forums/en/sqlanalysisservices/thread/cb5ec22b-65c0-4b86-9879-40e42c039808