Qlik Community

New to Qlik Sense

Discussion board where members can get started with Qlik Sense.

happyjoshua777
Contributor

min() does not transform all dates to their StartOfMonth equivalent in AGGR()

Hello,

IN order to aggregate data I used the following expression:

sum(

                aggr(

                  if(

              monthstart(min(Total <CustomerId,LocationId> RegistrationDate))=StartOfMonth,1,0

            )           

            ,CustomerId,LocationId,StartOfMonth

        )

         

    )


The data snapshot has been attached (refer to data_snapshot.png)

Initially, I had data for dates for 2016-2017 and the expression above worked perfectly well.

MIN() converted dates to their StartOfMonth equivalent.

However, when I added dates for the first six months of 2018 (see lower part of data_snapshot.png), only those RegistrationDate values which fall on the first of a given Month were accounted for.

in other words out of about a hundred dates which I added for 2018, only 20 are accounted for in the resulting Pivot Table resulting_table.png), while for 2016 and 2017 I get an accurate count for all Customers who registered throughout 2016-2017.

Note: StartOfMonth is product of MonthStart("Date") whereas Date is a column in the Calendar table which contains a range of all dates for 2016-2018 (refer to date_snapshot)



Question: how can I correct the SUM() expression above to ensure that all CustomerIDs for all years are accounted for for all RegistrationDate\s and not just the ones who happened to had registered on the first date of the month?

(Refer expected_result.png which was calculated using flags and SUM())


Thank you

12 Replies

Re: min() does not transform all dates to their StartOfMonth equivalent in AGGR()

I am a little confused... what really changed between 2016/2017 and 2018? The dates seems to be randomly spread out in month for 2016/2017 and 2018... why would the expression work for 2016/2017, but not 2018? It seems you understand the difference between the two... would you be able to elaborate on this a little bit more?

happyjoshua777
Contributor

Re: min() does not transform all dates to their StartOfMonth equivalent in AGGR()

Nothing changed between 2016/17, I just added random dates for 2018/01 until 2018/07.

Dates for 2016/17 were also generated at random previously.

The expression worked fine for two yearly periods 2016 and 2017 yet fails to aggregate correctly for the 97 (random) records for the first half of 2018.

thanks

Re: min() does not transform all dates to their StartOfMonth equivalent in AGGR()

I know you mentioned that StartOfMonth is created as a MonthStart field... but may be add MonthStart to StartOfMonth in the expression just to be sure

sum(

                aggr(

                  if(

              monthstart(min(Total <CustomerId,LocationId> RegistrationDate))=MonthStart(StartOfMonth),1,0

            )          

            ,CustomerId,LocationId,StartOfMonth

        )

        

    )

happyjoshua777
Contributor

Re: min() does not transform all dates to their StartOfMonth equivalent in AGGR()

tried MonthStart(StartOfMonth) and the same output.

I have attached a snapshot (see calendar.png) of qvd which I  generated from Calendar table for testing purposes previously.

As one can see StartOfMonth column was getting populated correctly in any case.

thanks

Re: min() does not transform all dates to their StartOfMonth equivalent in AGGR()

Nothing seems obviously wrong by looking at your expression and images, would you be able to share a sample to may be help you better?

happyjoshua777
Contributor

Re: min() does not transform all dates to their StartOfMonth equivalent in AGGR()

I have attached the following images with snapshots of data for 2018:

  • CustomerDetails_1.png
  • CustomerDetails_2.png
  • geography.png - refer to lookup() below
  • OnlineCustHist.png - customer transactions with LocationIds of the online stores purchases were made from

As I mentioned prev. it is only data for 2018 which is not getting aggregated correctly.

...

CustomerStats:

Load

    Year(RegistrationDate) & Num(Month(RegistrationDate)) as YearMonth,

        lookup('Country','LocationId',LocationId,'Geography') as RegistrationCountry,

        Count(CustomerId) as CountOfCust

Resident

CustomerDetails

    Group by

    lookup('Country','LocationId',LocationId,'Geography'),

        Year(RegistrationDate) & Num(Month(RegistrationDate));

thank you

Re: min() does not transform all dates to their StartOfMonth equivalent in AGGR()

Would you be able to provide the data you have provided as image as a Excel file? I might be able to load them to see what you mean, because images might take a long time to see what issue you might be having

Uploading a Sample

happyjoshua777
Contributor

Re: min() does not transform all dates to their StartOfMonth equivalent in AGGR()

I have uploaded snapshots of data in Excel

thanks

happyjoshua777
Contributor

Re: min() does not transform all dates to their StartOfMonth equivalent in AGGR()

customers_accounted_for_2018.png

These are the Customer Ids which were accounted for by the AGGR expression above.

Community Browser