The default behaviour in Marshmallow upwards is to charge and other options have to be manually selected
Being an OS behaviour, you can't modify it without rooting device. If your device is rooted, you can install this Xposed module - USB for Marshmallow
Set MTP or PTP as Default USB Connection on Marshmallow with this app.
Once you connect the phone to the computer and select the USB option from the notification and selected option will be retained for every reconnect your phone to computer.
For example if you select the USB file transfer option, This app will retain that file transfer option for every reconnect your phone to computer.
It's also available on Play Store as an Xposed app
If you are on Nougat, this option may not be possible till Xposed framework is released - see Where can I find the Xposed Framework and installer for Nougat?