# 14 Count Conversions.r

  ## Ensure the two DT's are identical
  if (!identical(DT, DT.Spotify.Counts.plusMAX_MIN_DATE.byUCnDPMb)) {
    warning ("Not identical at line No.4, file 14\n")
    DT.Spotify.Counts.plusMAX_MIN_DATE.byUCnDPMb <- DT
  }

DT2 <- copy(DT[1:5e5, 1:8, with=FALSE])

  kCols_DCnPU_lastDate_Mobile <- c(kCols_DCnU, "lastStream_byUCnDPMb", "mobile") ## NO PRODUCT!!
  setkeyIfNot(DT2, kCols_DCnPU_lastDate_Mobile)
  DT2[, productDELTA := diffNA(product, padTop=TRUE), by=userid]
  setcolorderpt(DT2, c(kCols_DCnUP, "productDELTA", "mobile"))


  DT2[!is.na(productDELTA), deltasPerU := lunique(productDELTA), by=userid]
  DT2[deltasPerU==3] [order(userid, lastStream_byUCnDPMb)  ]
  DT2[userid=="0e121d5458a4bff78dab82bc3fd64f4f"]


tabled <- as.data.table(DT2[, table(product, userid)])
tabled <- as.data.table(dcast(tabled, userid~product))
tabled[, total := rowSums(tabled[, -1, with=FALSE])]

.by <- structure(list(month = structure(15765, class = "Date"), country = structure(1L, .Label = c("AD", "AR", "AT", "AU", "BE", "CH", "DE", "DK", "EE", "ES", "FI", "FR", "GB", "GR", "HK", "IE", "IS", "IT", "LI", "LT", "LU", "LV", "MC", "MX", "MY", "NL", "NO", "NZ", "PL", "PT", "SE", "SG", "TR", "TW", "US"), class = "factor"), product = structure(2L, .Label = c("Daypass", "Open", "Free", "Limited", "DesktopBasic", "Premium"), class = "factor")), .Names = c("month", "country", "product"))
ERR <- DT[]


key(DT.Spotify.Counts.plusMAX_MIN_DATE.byUCnDPMb)


## Conversion
{
  ### Plan can be changed for three reasons
  ###    (1)  Conversion  (Product changed)
  ###    (2)  Moved       (Country changed)
  ###    (3)  Device Use  (Mobile  changed)

  ## Find customers with more than one product tier
  DT.Spotify.Counts.plusMAX_MIN_DATE.byUCnDPMb

  DT.UserProduct <- DT.mini[, list(MonthsUsed=.N), keyby=list(userid, product)]
  Customers.withMultiProduct <- DT.UserProduct[, .N, by=userid][N>1, userid]

  setkeyIfNot(DT.mini, "userid")

  ## Note to self, this method is faster than alternative. See Benchmark file
  DT.Upgraders <- DT.mini[Customers.withMultiProduct]
  cleanSpotify_(DT.Upgraders)
  DT.Upgraders <- DT.Upgraders[, list( year[-1L], month[-1L]
                           , ProductFrom = product[- (.N) ]
                           , ProductTo = product[-1L]
                           , ProductDiff=diff(as.integer(product))
                          )
                    , keyby=userid]
  colsOrdered <- c("userid", "year", "month", "ProductFrom", "ProductTo", "ProductDiff")
  setcolorderpt(DT.old, colsOrdered)

  jesusForData(DT.Upgraders)
  notify("DONE")
}


======










         month country                           userid product mobile lastStream_byUCnDPMb total_streams_perCnDPUMb isLastOccurOfUser_perD
 1: 2013-03-01      ES 00462337ecd6149934103c1197e8302f    Free   TRUE                      2013-03-18                        4                  FALSE
 2: 2013-03-01      ES 00462337ecd6149934103c1197e8302f    Free  FALSE                      2013-04-01                      107                   TRUE
 3: 2013-03-01      ES 00462337ecd6149934103c1197e8302f Premium   TRUE                      2013-03-17                        5                  FALSE
 4: 2013-03-01      ES 00462337ecd6149934103c1197e8302f Premium  FALSE                      2013-03-18                       19                  FALSE
 5: 2013-04-01      ES 00462337ecd6149934103c1197e8302f    Free  FALSE                      2013-04-16                       92                   TRUE
 6: 2013-05-01      ES 00462337ecd6149934103c1197e8302f    Free  FALSE                      2013-05-31                       30                   TRUE
 7: 2013-06-01      ES 00462337ecd6149934103c1197e8302f    Free  FALSE                      2013-06-12                        3                   TRUE
 8: 2013-08-01      ES 00462337ecd6149934103c1197e8302f    Free  FALSE                      2013-08-18                        6                   TRUE
 9: 2013-10-01      ES 00462337ecd6149934103c1197e8302f    Free  FALSE                      2013-10-31                       86                   TRUE
10: 2013-11-01      ES 00462337ecd6149934103c1197e8302f    Free  FALSE                      2013-11-30                       95                   TRUE
11: 2013-12-01      ES 00462337ecd6149934103c1197e8302f    Free  FALSE                      2013-12-31                      161                   TRUE
12: 2014-01-01      ES 00462337ecd6149934103c1197e8302f    Free  FALSE                      2014-02-01                      162                   TRUE
13: 2014-02-01      ES 00462337ecd6149934103c1197e8302f    Free  FALSE                      2014-02-10                       68                   TRUE