Does the battery condition show "Service Recommended" or "Normal"?
Try running the Apple Diagnostics to see if any hardware issues are detected.
If the battery condition is "Service Recommended", then have the laptop serviced to have the battery replaced.
Usually if the laptop suddenly powers off at anything above 5% and is at 0% charge when powered back on, then it usually indicated the battery is failing. However, the battery condition and the Apple Diagnostics may still show everything is Ok. Sometime checking when the battery charge is low will change those results. If you can show the laptop suddenly powering off at 30% charge, then Apple will most likely replace the battery even if the diagnostics & battery condition report normal.