I think you are making no mistakes but the grouping is lost by the unnecessary as_tibble.
See annotations here and the full reprex below that.
Because you did not mention which package you use for the analysis of the anomalies I can not guess the impact of this.
Test.df2$Period<-lubridate::my(Test.df2$Period)
Test.df2[is.na(Test.df2)]<-0 # ???
Test.df2<-Test.df2 %>% group_by(Product,Location)
class(Test.df2) # already a tibble
print(Test.df2) # this shows it is a grouped tibble
glimpse(Test.df2) # and here
Test.df2<-as_tibble(Test.df2) # not necessary (already a tibble) but ...
class(Test.df2) # a tibble but no longer grouped
print(Test.df2) # as show here
glimpse(Test.df2) # and here
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(tibble)
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>
#> date, intersect, setdiff, union
Test.df2<-structure(list(Product = c("A1", "A1", "A1", "A1", "B1", "B1", "B1", "B1"),
Location = c("A", "A", "A", "A", "A", "B", "A", "C"),
Period = c("Sep 2017", "Oct 2017", "Nov 2017", "Dec 2017", "Sep 2017", "Oct 2017", "Nov 2017", "Dec 2017"),
Units = c(5.399993, 9, 10, 9, 5, 1, 3, 1)), row.names = c(NA, 8L), class = "data.frame")
Test.df2$Period<-lubridate::my(Test.df2$Period)
Test.df2[is.na(Test.df2)]<-0 # ???
Test.df2<-Test.df2 %>% group_by(Product,Location)
class(Test.df2) # already a tibble
#> [1] "grouped_df" "tbl_df" "tbl" "data.frame"
print(Test.df2) # this shows it is a grouped tibble
#> # A tibble: 8 x 4
#> # Groups: Product, Location [4]
#> Product Location Period Units
#> <chr> <chr> <date> <dbl>
#> 1 A1 A 2017-09-01 5.40
#> 2 A1 A 2017-10-01 9
#> 3 A1 A 2017-11-01 10
#> 4 A1 A 2017-12-01 9
#> 5 B1 A 2017-09-01 5
#> 6 B1 B 2017-10-01 1
#> 7 B1 A 2017-11-01 3
#> 8 B1 C 2017-12-01 1
glimpse(Test.df2) # and here
#> Rows: 8
#> Columns: 4
#> Groups: Product, Location [4]
#> $ Product <chr> "A1", "A1", "A1", "A1", "B1", "B1", "B1", "B1"
#> $ Location <chr> "A", "A", "A", "A", "A", "B", "A", "C"
#> $ Period <date> 2017-09-01, 2017-10-01, 2017-11-01, 2017-12-01, 2017-09-01, 2~
#> $ Units <dbl> 5.399993, 9.000000, 10.000000, 9.000000, 5.000000, 1.000000, ~
Test.df2<-as_tibble(Test.df2) # not necessary (already a tibble) but ...
class(Test.df2) # a tibble but no longer grouped
#> [1] "tbl_df" "tbl" "data.frame"
print(Test.df2) # as show here
#> # A tibble: 8 x 4
#> Product Location Period Units
#> <chr> <chr> <date> <dbl>
#> 1 A1 A 2017-09-01 5.40
#> 2 A1 A 2017-10-01 9
#> 3 A1 A 2017-11-01 10
#> 4 A1 A 2017-12-01 9
#> 5 B1 A 2017-09-01 5
#> 6 B1 B 2017-10-01 1
#> 7 B1 A 2017-11-01 3
#> 8 B1 C 2017-12-01 1
glimpse(Test.df2) # and here
#> Rows: 8
#> Columns: 4
#> $ Product <chr> "A1", "A1", "A1", "A1", "B1", "B1", "B1", "B1"
#> $ Location <chr> "A", "A", "A", "A", "A", "B", "A", "C"
#> $ Period <date> 2017-09-01, 2017-10-01, 2017-11-01, 2017-12-01, 2017-09-01, 2~
#> $ Units <dbl> 5.399993, 9.000000, 10.000000, 9.000000, 5.000000, 1.000000, ~
Created on 2021-09-15 by the reprex package (v2.0.0)