different part of where condition apply to different part of mdx queries - where-clause

I'm new in mdx, i have question about put two condition in where to apply in different part of mdx queries.
in my query i have two set(include from date:to date) that i want apply first set to first member and second set to second member but i don't want to create tuple in member because i want to calculate sum of measure for every customer.
when i put condition in where, just one date apply to member I want apply different condition apply to different member.
how can I do this?
WITH
SET BaseDate AS
[Vw Dim Date].[Shamsi Date].&[1396/02/22] : [Vw Dim Date].[Shamsi Date].&[1396/03/19]
SET CompareDate AS
[Vw Dim Date].[Shamsi Date].&[1396/01/21] : [Vw Dim Date].[Shamsi Date].&[1396/02/19]
MEMBER TotalCustomerCntInBase AS
Sum(BaseDate.item(count(BaseDate)-1) ,[Measures].[Total Customer Cnt])
MEMBER TotalCustomerCntInCompare AS
Sum(CompareDate.item(count(CompareDate)-1) ,[Measures].[Total Customer Cnt])
member numberOfActivecustomers as
count(filter (nonempty([Vw Customer].[Customer BK].[Customer BK],[Measures].[Trade Cnt]),[Measures].[Trade Cnt]=1))
select {numberOfActivecustomers} on 0
from [DV Present]
where ([Vw Dim Date].[Shamsi Date].&[1395/01/01]:[Vw Dim Date].[Shamsi Date].&[1395/02/01])

In order to sum days you may create two calculated members (periods) and run any measure against them:
With
Member [Vw Dim Date].[Shamsi Date].[BaseDate] as
Sum(
[Vw Dim Date].[Shamsi Date].&[1396/02/22]:
[Vw Dim Date].[Shamsi Date].&[1396/03/19]
)
Member [Vw Dim Date].[Shamsi Date].[CompareDate] as
Sum(
[Vw Dim Date].[Shamsi Date].&[1396/01/21]:
[Vw Dim Date].[Shamsi Date].&[1396/02/19]
)
// Add here any calculations you need ...
Select
{[Measures].[Trade Cnt]} on 0,
{[Vw Dim Date].[Shamsi Date].[CompareDate],[Vw Dim Date].[Shamsi Date].[CompareDate]} on 1
from [DV Present]

Related

How to compare a variable with a pivot field

I want to compare my variable with a pivot field.
Here's what I got:
Dim ptField As PivotField
Dim Table As PivotTable
Dim Field As String
Dim Pos As Integer
Dim ptr_Field as String
Set Table = Sheets("Sheet1").PivotTables(1)
Set ptr_Field = "Value1"
For Each Stat In Table.PivotFields
If ptr_Field.Name = Stat Then
MsgBox "True"
End If
Next
But it doesn't work.
Can someone help me please?
You have defined ptField as PivotField but you never reference it and instead refer to Stat which you have not defined at all. You have also defined Field and Pos which never get used.
Also, you are trying to reference ptf_Field.Name which would imply that ptf_Field was an object with a property called Name when in fact it is defined as a String.
Try:
For Each ptField in Table.PivotFields
If ptf_Field = ptField.Value Then
MsgBox "True"
End If
Next

Initiating variables, of different types, in a way that I can iterate through them with a variable

'Declare variables for cell values attained from APR spreadsheet
Dim orgSheetvalues(0) As String 'Project Title
Dim orgSheetvalues(1) As String 'Circuit Tag
Dim orgSheetvalues(2) As String 'District
Dim orgSheetvalues(3) As String 'State
Dim orgSheetvalues(4) As Date 'Date recieved
Dim orgSheetvalues(5) As Currency 'Planned Capital Cost
Dim orgSheetvalues(6) As Currency 'Actual Capital Cost
Dim orgSheetvalues(7) As Date 'Capital work completed date
Dim orgSheetvalues(8) As Currency 'Planned O&M Cost
Dim orgSheetvalues(9) As Currency 'Actual O&M Cost
Dim orgSheetvalues(10) As Date 'O&M work completed date
Dim orgSheetvalues(11) As String 'RWP File Path
Clearly, this does not work. Does anyone know of a way I can use the same variable name, for different variable types, in a way that can be iterated as the above would if it worked (eg. orgSheetvalue(iterating variable))?
While the Variant array will work just fine, it's prone to errors as you will need to remember the datatype each position holds.
A custom Type in my opinion would be better approach.
Public Type MyCustomType
ProjectTitle As String
CircuitTag As String
District As String
State As String
DateRecieved As Date
PlannedCapitalCost As Currency
'...
End Type
Sub T()
Dim o As MyCustomType
o.ProjectTitle = "abc"
o.CircuitTag = "..."
o.DateRecieved = Date
End Sub
example
Option Explicit
Sub aaa()
Dim bbb(10) As Variant
bbb(0) = "test 123" ' string
Set bbb(1) = Range("a1") ' object
bbb(2) = 98765 ' numeric
Debug.Print bbb(0); vbTab; bbb(1).Interior.Color; vbTab; bbb(2)
End Sub

How do I do func( of datarow)

This should be easy but I'm not getting it. After an hour of Googling and MSDN perusing I'm still not getting it.
I want to sum an entire column in a typed datatable using Linq.
Won't compile:
Dim sum4 As Decimal = PmtList.Sum(x System.Func(Of GSRDataSet.PmtHistRow)())
Won't compile:
Dim x As Decimal
Dim sum3 = PmtList.AsEnumerable().Sum(x >= x.Field < Int() > ("Balance"))
Also tried: Got error "missing operand"
Dim sumObj As Object = PmtList.Compute("Sum(Total)", "=<>0")
How do I sum an entire column in a datatable using Linq?
If your Balance field is of type Decimal then you could do it like this:
Dim sum As Decimal = PmtList.AsEnumerable().Sum(Function(row As DataRow) row.Field(Of Decimal)("Balance"))
This should work:
Dim sum3 = PmtList.AsEnumerable().Sum(Function(x) x.Field(Of Int)("Balance"))

Linq - Range variable hides a variable in an enclosing block

I have several Linq statements in a public procedure (here are 2 of them):
'this one gets us table like this: "number of tickets, day"
Dim ticketsCreated = From ticket In CreatedTickets
Group ticket By GroupKey = New With {Key .IssueDate = CDate(ticket.Date).Date}
Into g = Group Select New
With {Key .IssuesCount = g.Count(), Key .IssueDate = GroupKey.IssueDate}
'this one gets us table like this: "number of tickets, day"
Dim ticketsClosed = From ticket In ClosedTickets
Group ticket By GroupKey = New With {Key .IssueDate = CDate(ticket.Date).Date}
Into g = Group Select New
With {Key .IssuesCount = g.Count(), Key .IssueDate = GroupKey.IssueDate}
I get the follwoing error on GroupKey in both statements:
Range variable hides a variable in an enclosing block, a previously defined range variable,
or an implicitly declared variable in a query expression
I do not have a variable named GroupKey declared anywhere outside of my Linq statements.

DataTable to ArrayList

I want to get value from datatable and then store it to the string() arraylist. datatable contain 3 column (columnA | columnB | columnC). I want to get all value from columnB and store it to arraylist.
I have try something like this
If myTableData.Rows.Count > 0 Then
For i As Integer = 0 To myTableData.Rows.Count - 1
Dim value() As String = myTableData.Rows(i)(1)
Next
End If
but when I compile that code, I got error message like this :
Unable to cast object of type 'System.String' to type 'System.String[]'
please help me.....
You could do that with LINQ:
Dim colBValues = (From row In myTableData Select colB = row(1).ToString).ToList
Or if you prefer the "old-school" way:
Dim colBValues = New List(Of String)
For Each row As DataRow In myTableData.Rows
colBValues.Add(row(1).ToString)
Next
I've used a List(Of String) because it's type-safe, therefore you don't need to cast the values everytime. That makes code more readable, more failsafe and faster.
If you need it as String-Array, you could simply use ToArray:
Dim colBValues = (From row In myTableData Select colB = row(1).ToString).ToArray
List(T)
LINQ
Dim a() As String
Dim total As Integer
'Count the number of rows
total = myTableData.Rows.Count - 1
ReDim a(0 To total)
For i = 0 To total
a(i) = myTableData.Rows(i)(1)
Next
This is really old question, but I'll provide one more variant of answer :
Dim value = myTableData.Rows.OfType(Of DataRow).Select(Function(x) x(1).ToString()).ToArray
So, if You have more than one column in Your DataTable You can use x(columnIndex) instead 1.